我在我的Wordpress主题的function.php文件中实现了以下Wordpress过滤器:

function change_oembed($html, $url, $args){

   $video_pattern = "#(\W|^)(youtube)(\W|$)#";
   $notification = '<div class="cookieconsent-optout-marketing">Please 
                    <a href="javascript:Cookiebot.renew()">accept marketing-cookies</a> 
                   to watch this video.
                   </div>';


   if(preg_match($video_pattern, $url)){                

    $matches = null;
    preg_match("/^(?:http(?:s)?:\/\/)?(?:www\.)?(?:m\.)?(?:youtu\.be\/|youtube\.com\/(?:(?:watch)?\?(?:.*&)?v(?:i)?=|(?:embed|v|vi|user)\/))([^\?&\"'>]+)/", $url, $matches);

    $gdpr_youtube = '<iframe data-src="https://www.youtube.com/embed/' .
                     $matches[1] . '" data-cookieconsent="marketing" frameborder="0"
                     allowfullscreen></iframe>';


    $new_html = '<div class="clearfix"></div>' . $notification . $gdpr_youtube;
 } else {
    $new_html = $html;

}

return $new_html;
}

 add_filter( 'oembed_result', array($this, 'change_oembed'), 999, 3 );

我可以在帖子中看到这个过滤器的结果 . 我只看到youtube.com视频网址文字 . 我尝试使用wordpress和浏览器(使用FastCache插件)清除缓存,我尝试将代码放在if-else语句之外但没有任何作用 .

update: 我也尝试了 video_embed_html 而不是 oembed_result 但它仍然不起作用 .

Update 2: 未调用该函数 . 我只是提出一些问号,看看我是否在代码中看到它们,但我没有 . 所以根本没有调用 change_oembed 函数 .

我检查了正则表达式是否有效,它确实适用于该YouTube网址,因此它不是正则表达式问题 .