我正在使用一个短码来显示来自自定义字段"tube_video_url“的视频。现在我想做的是自动将mqdefault图片添加到同一个视频的自定义文件"fifu_image_url“中。
我为自己做了这个简短的代码...
function video( $atts, $content = "" ) {
$string = get_post_meta(get_the_ID(), 'tube_video_url', TRUE);
$search = '/youtube\.com\/watch\?v=([a-zA-Z0-9]+)/smi';
$replace = 'youtube.com/embed/$1';
$content = preg_replace($search, $replace, $string);
return '<iframe id="video-frame" width="560" height="315" src="'.$content.'" frameborder="0" allowfullscreen></iframe>';
}
add_shortcode( 'video', 'video' );现在我不知道怎么把这个..。
https://www.youtube.com/watch?v=2yJS-byLHrU
进入这个..。
https://i.ytimg.com/vi/2yJS-byLHrU/mqdefault.jpg
我试过这样的东西
$string = 'https://www.youtube.com/watch?v=2yJS-byLHrU';
$search = '/youtube\.com\/watch\?v=([a-zA-Z0-9]+)/smi';
$replace = 'i.ytimg.com/vi/$1/mqdefault.jpg';
$content = preg_replace($search, $replace, $string);
echo $content;结果转到https://www.i.ytimg.com/vi/2yJS/mqdefault.jpg-byLHrU,注意-byLHrU是如何放在jpg后面的,还有当我按下提交按钮或第一次访问时,如何用这个值填充自定义字段“fifu_image_url”?
发布于 2019-09-10 12:41:52
您可以使用url提取youtube视频id,如下所示
$link = "https://www.youtube.com/watch?v=2yJS-byLHrU";
$video_id = explode("?v=", $link);
if (empty($video_id[1]))
$video_id = explode("/v/", $link);
$video_id = explode("&", $video_id[1]);
$video_id = $video_id[0]; // will print the output -> 2yJS-byLHrU发布于 2019-09-10 15:18:43
找到答案了,如果以后有人需要,我会把它贴出来…
/* Do something with the data entered */
add_action( 'save_post', 'myplugin_save_postdata' );
/* When the post is saved, saves our custom data */
function myplugin_save_postdata( $post_id ) {
// First we need to check if the current user is authorised to do this action.
if ( 'page' == $_POST['post_type'] ) {
if ( ! current_user_can( 'edit_page', $post_id ) )
return;
} else {
if ( ! current_user_can( 'edit_post', $post_id ) )
return;
}
$url = get_post_meta(get_the_ID(), 'tube_video_url', TRUE);
preg_match('%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', $url, $match);
$youtube_id = $match[1];
$mydata = 'https://i.ytimg.com/vi/'. $youtube_id. '/mqdefault.jpg'; // Do something with $mydata
update_post_meta( $post_id, 'fifu_image_url', $mydata );
}不幸的是,这只适用于从后端发布。
https://stackoverflow.com/questions/57863996
复制相似问题