我的主题使用 wp_oembed_get 通过 iframe 嵌入 YouTube 视频。
<?php echo wp_oembed_get( get_post_meta( get_the_ID(), 'wpex_post_video_oembed', true ) ); ?>
可以给 iframe 一个 id 吗?我需要它,所以使用 youtube api。
谢谢
最佳答案
您可以使用过滤器 oembed_dataparse
:
add_filter( 'oembed_dataparse', function( $return, $data, $url ){
if( false === strpos( $return,'youtube.com' ) )
return $return;
$id = explode( 'watch?v=', $url );
$add_id = str_replace( 'allowfullscreen>', 'allowfullscreen id="yt-'.$id[1].'">', $return );
return $add_id;
}, 10, 3 );
这是收到参数的快照(“结果”显示另一个过滤器,oembed_result
,但 dataparse 似乎更完整):
关于php - 在 WordPress 中使用 wp_oembed_get 为 youtube iframe 设置一个 id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26437934/