2

On the post editor field, I have added an iframe code. But on the frontend, it is not showing the content with the iframe code.

I have added the below code to print the excerpt on the blog page:

<?php echo get_post_field('post_content', $post['id']) ; ?>

It is not in the single post page, but I have tried to print the post content instead of excerpt on the Blog page.

Can you please point me where I am making the mistake?

5
  • Have you tried: <?php echo wp_kses_post(get_post_field('post_content', $post['id']) ); ?> I think without the wp_kses_post() it might be getting stripped out.
    – Faye
    Jul 28 at 19:54
  • 2
    You're requesting the post_content when you should be requesting the post_excerpt.
    – vancoder
    Jul 28 at 22:24
  • @vancoder I am calling the complete post content instead f excerpt as I need all but still the content is getting filtered
    – Jituu
    Jul 29 at 6:35
  • @Faye That is not working either
    – Jituu
    Jul 29 at 6:35
  • WP doesn't support putting iframe tags directly into WP content, the code in your question is not printing the excerpt though
    – Tom J Nowell
    Aug 4 at 22:15

2 Answers 2

0

By default, Wordpress strips html tags from the excerpt.

There is a previous question with a thorough answer which will help you implement this properly on this page.

0

I believe this will allow iframes through wp_kses_post, but I haven't personally tried it. source

/**
 * Add iFrame to allowed wp_kses_post tags
 *
 * @param array  $tags Allowed tags, attributes, and/or entities.
 * @param string $context Context to judge allowed tags by. Allowed values are 'post'.
 *
 * @return array
 */
function custom_wpkses_post_tags( $tags, $context ) {

    if ( 'post' === $context ) {
        $tags['iframe'] = array(
            'src'             => true,
            'height'          => true,
            'width'           => true,
            'frameborder'     => true,
            'allowfullscreen' => true,
        );
    }

    return $tags;
}

add_filter( 'wp_kses_allowed_html', 'custom_wpkses_post_tags', 10, 2 );

Just to flag though - due to a comment on this answer - this isn't the best way to go about adding video to post content. If you're using Gutenberg, there are native embed blocks, and post_content should be able to parse it without any security vulnerabilities. Or you could create an ACF block for your embed code using the acf oembed field, and utilize block parsing to create your excerpt.

2
  • 1
    allowing arbitrary iframes in post content is dangerous, it's safer to use a block/shortcode/OEmbed to implement an iframe
    – Tom J Nowell
    Aug 4 at 22:16
  • Oh I agree, there are MUCH better ways to include video embeds in your content, but if content entry is adding embeds I'm answering the question, not trying to force OP into best practice.
    – Faye
    Aug 5 at 15:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.