I'm running through the Theme Checker plugin for my Wordpress theme, and I have the following code snippets:
1. Get and show only the first embedded video from the post content:
<?php $content = apply_filters( 'the_content', $post->post_content );
$embeds = get_media_embedded_in_content( $content );
$first_embedded = $embeds[0];
echo $first_embedded;
?>
2. Remove any embedded content from the post content:
<?php
$content = apply_filters( 'the_content', $post->post_content );
$content = preg_replace("/(<iframe[^<]+<\/iframe>)/", '', $content);
echo $content;
?>
I'm getting this warning in the theme Checker:
WARNING: Found echo $ in the file single.php. Possible data validation issues found. All dynamic data must be correctly escaped for the context where it is rendered.
Line 34: echo $first_embedded;
Line 76: echo $content;
How may I properly escape those variables? I've tried with:
<?php esc_html( $first_embedded); ?>
But it just prints the HTML <iframe>...</iframe>
code of my embedded video, like if it was a simple text string.
Same thing if I do it on the $content
:
<?php esc_html( $content); ?>
I'm a beginner. The problem is that I need those 2 variables because the first one shows the video in case the post format is a Video type and replaces the post thumbnail image with the first embedded video.
The second function is needed as well in order not to show that video in the post's content.