我正在运行我的 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); ?>
我是初学者。问题是我需要这两个变量,因为第一个变量显示视频(如果帖子格式是视频类型),并用第一个嵌入视频替换帖子缩略图。
还需要第二个函数,以便不在帖子内容中显示该视频。
最佳答案
我找到了一个很好的解决方法,可以在 Theme Checker 上顺利运行,当然在 WordPress 页面上也可以顺利运行,即使用 html_entity_decode()
所以我更换了:
echo $first_embedded;
用这一行:
echo html_entity_decode( esc_html( $first_embedded ) );
还有:
echo $content;
与:
echo html_entity_decode( esc_html($content) );
html_entity_decode()
函数将 HTML 实体转换为字符,这就是我们需要显示过滤后的帖子内容的代码并在 Envato 或 WP 主题检查器中没有错误的代码。
关于iframe - 如何正确转义过滤后的内容变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55953774/