gpt4 book ai didi

php - 在Wordpress帖子中捕获Youtube视频并插入自定义字段值

转载 作者:行者123 更新时间:2023-12-03 05:50:48 27 4
gpt4 key购买 nike

谁能帮帮我,我需要扫描该帖子以获取youtube视频。如果帖子包含视频(www.youtube.com/embed/youtubepostid),那么我想使用键“iFrame” 将该网址复制到自定义字段中。

我绕圈寻找答案,每当我尝试抚慰时,我就会对WSD感到满意:(

任何指向正确方向的指针?

最佳答案

我已经编写了一些代码来解决我们的常见问题;)
如果您要显示格式化的嵌入视频(无论帖子内容如何),也可以添加一些内容。

I-将这些功能添加到“functions.php”文件中

1-从内容中删除嵌入的youtube

function remove_video( $content ) {
$postOutput = preg_replace('/<iframe[^>]+./','', $content);
return $postOutput;
}
add_filter( 'the_content', 'remove_video', 100 );

2-从内容中捕获并返回嵌入的youtube
function catch_first_youtube_video() {
global $post, $posts;
// Catch youtube iframe src
preg_match_all('/(http|https).*(yout).*/i', $post->post_content, $matches);
$youtubeUrl = $matches[0][0];

// Set the new meta for your post.
add_post_meta($post->ID, 'iFrame', $youtubeUrl)

// Remove http: or https:
$formated = preg_replace('/(http:|https:|watch\\?v=)/i','', $youtubeUrl);

// Formated string '//youtube.com/embed/XXXXXXX'
$formated =str_replace('youtube.com/','youtube.com/embed/', $formated );

return '<iframe src="'.$formated .'"></iframe>';
}

II-在您的帖子模板内容中调用这些函数

1-在帖子模板中显示帖子的内容,但不嵌入视频
remove_filter(the_content(),'remove_video', 100 );

2-根据需要显示嵌入的视频iframe!
<?= catch_first_youtube_video(); ?>

和瞧!请享用 ! (对不起,我的英语不好)

关于php - 在Wordpress帖子中捕获Youtube视频并插入自定义字段值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25205342/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com