如何正确逃脱过滤的内容变量



我正在通过主题Checker插件的WordPress主题运行,并且我有以下代码片段:

1。仅获取帖子内容中的第一个嵌入式视频:

<?php  $content = apply_filters( 'the_content', $post->post_content );
$embeds = get_media_embedded_in_content( $content );
$first_embedded = $embeds[0];
echo $first_embedded;
  ?>

2。从帖子内容中删除任何嵌入式内容:

   <?php 
     $content = apply_filters( 'the_content', $post->post_content );
     $content = preg_replace("/(<iframe[^<]+</iframe>)/", '', $content);
     echo $content;
     ?>

我在主题检查器中收到此警告:

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;

我如何正确逃脱这些变量?我尝试了:

<?php esc_html( $first_embedded); ?>

但它只是打印我嵌入式视频的HTML <iframe>...</iframe>代码,就像是简单的文本字符串。

同样的事情,如果我在$content上这样做:

 <?php esc_html( $content); ?>

我是初学者。问题是我需要那两个变量,因为第一个变量显示了视频,以防帖子格式是视频类型,并用第一个嵌入式视频替换了缩略图映像。

也需要第二个功能,以便不显示帖子内容中的视频。

我找到了一个不错的解决方法,它在主题检查器上也顺利进行,当然也可以在WordPress页面上进行,这是通过使用 html_entity_decode((

所以我替换了:

echo $first_embedded;

使用此行:

echo html_entity_decode( esc_html( $first_embedded ) );

和:

echo $content;

with:

echo html_entity_decode( esc_html($content) );

html_entity_decode()函数将HTML实体转换为字符,这是我们需要显示过滤后的帖子内容的代码,并且在Envato或WP主题检查器中没有错误。

最新更新