php – WordPress自动嵌入YouTube视频 – 添加过滤器来处理`end`属性

WordPress会自动将页面/帖子内容中的YouTube网址转换为嵌入式iframe视频.

它尊重YouTube网址中的起始参数(如果存在),但它不尊重结束参数(如果存在).

因此,我需要找到处理这种自动YouTube嵌入功能的WordPress代码,以便我可以希望在我自己的过滤器中挂钩(using this solution)将处理最终要求.

我搜索了/ wp-includes /目录的class-wp-embed.php,class-oembed.php和media.php文件,后者认为我找到了我需要的代码…

apply_filters(‘wp_embed_handler_youtube’,$embed,$attr,$url,$rawattr)

…但是这个过滤器似乎没有被调用.

谁能指出我正确的方向?

解决方法:

我有同样的问题,但没有找到答案.所以这是工作解决方案:

add_filter('embed_oembed_html', 'my_theme_embed_handler_oembed_youtube', 10, 4);
function my_theme_embed_handler_oembed_youtube($html, $url, $attr, $post_ID) {
    if (strpos($url, 'youtube.com')!==false) {
        /*  YOU CAN CHANGE RESULT HTML CODE HERE */
        $html = '<div class="youtube-wrap">'.$html.'</div>';
    }
    return $html;
}
上一篇:博客园个人主页添加音乐插件


下一篇:php – 最好的Youtube嵌入播放器尺寸?