当我们在WooCommerce中嵌入Kingsway视频时,我会碰到一些问题,下面的内容将帮助你解决:
1. 原因
WordPress自动对内容做了过滤和格式化导致的。
在 WooCommerce 的 product description(商品描述)里,WordPress 默认会把内容当作“post content”处理,而 wpautop 和 wp_kses 这些函数会把 <script> 包起来,甚至直接过滤掉。
2. 解决方法
1. 安装Code Snippets插件
在wordpress后台安装Code Snippets插件,这个是一个免费的,非常安全和稳定的,下载和安装量超过100万的插件。
2. 添加代码
Snippet Title那里可以填写(这个信息你可以随意填写):
allow script tags in video_embed_code
Snippet Content那里填写:
function allow_script_in_description($tags) {
$tags['script'] = array(
'type' => true,
'src' => true,
'async' => true,
'defer' => true
);
return $tags;
}
add_filter('wp_kses_allowed_html', 'allow_script_in_description', 10, 2);
function disable_wpautop_on_products($content) {
if (is_singular('product')) {
remove_filter('the_content', 'wpautop');
remove_filter('the_content', 'wptexturize');
}
return $content;
}
add_filter('the_content', 'disable_wpautop_on_products', 0);3. 保存并测试
保存后即可,然后可以去到之前内嵌了Kingsway的视频的页面进行查看。
