iframe - 如何正确转义过滤后的内容变量?

标签 iframe filter escaping embed

我正在运行我的 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/

相关文章:

bash - 在 Bash 中使用 sed 转义域名中的点

java - 不要在字符串中转义双引号

caching - Symfony 过滤器在 Controller 之前运行并可能跳过 Controller

excel - 根据用户名筛选 excel

javascript - 如何使用javascript获取文本输入中的html特殊字符

javascript - 如何允许通过触摸屏放大和缩小谷歌浏览器中的iframe

css - :after pseudoelement is cropped when using filter in IE8

javascript - 更改一个 iframe 的 url 从另一个

javascript - 隐藏/显示 Ipresso 生成的表单内的内容

javascript - iframe 中父级到子级的跨域访问