我试图在添加/更新新的自定义帖子类型视频时,自动添加Vimeo或YouTube的视频缩略图作为特色图像。视频ID是一个高级自定义字段。以下是我的非工作代码:
/**
* Gets a vimeo thumbnail url
* @param mixed $id A vimeo id (ie. 1185346)
* @return thumbnail\'s url
*/
function getVimeoThumb($id) {
$data = file_get_contents("http://vimeo.com/api/v2/video/".$id.".json");
$data = json_decode($data);
return $data[0]->thumbnail_large;
}
/**
* Gets a youtube thumbnail url
* @param mixed $id A youtube id
* @return thumbnail\'s url
*/
function getYoutubeThumb($id) {
$data = file_get_contents("https://www.googleapis.com/youtube/v3/videos?key=AIzaSyB-gLbxJIJCGEwmDlC3sE9ml5DJ3tAaWnE&part=snippet&id=".$id);
$data = json_decode($data);
return $json->items[0]->snippet->thumbnails->medium->url;
}
// Add featured image for videos
function addVideoThumb($post_ID) {
if(!has_post_thumbnail($post_ID)):
if(get_field("source", $post_ID) == "youtube"):
$videoID = get_field("youtube_video_id", $post_ID);
$imageURL = getYoutubeThumb($videoID);
else:
$videoID = get_field("vimeo_video_id", $post_ID);
$imageURL = getVimeoThumb($videoID);
endif;
//echo $imageURL;
// download image from url
$tmp = download_url($imageURL);
$file = array(
\'name\' => basename($imageURL),
\'tmp_name\' => $tmp
);
// create attachment from the uploaded image
$attachment_id = media_handle_sideload($file, $post_ID);
// set the featured image
update_post_meta($post_ID, \'_thumbnail_id\', $attachment_id);
endif;
}
add_action(\'save_post_interel-tv-videos\', \'addVideoThumb\', 10, 3);
当我尝试向其添加JavaScript警报时,该函数似乎根本没有被触发,但没有弹出任何消息。有什么帮助吗?提前感谢!