There are a few steps you need to cover.
- Get ID from MetaBox embed
- Pull list of available sizes based on ID
- Import image into your library if doesn't already exist
- Attach new file to post.
STEP 2 | YouTube
Generate an array of sizes from YouTube to use later:
function getYoutTubeImagesFromID($yt_id, $prefix = 'https:') {
$yt_url = 'img.youtube.com'; // can be 'i3.ytimg.com'
$sizes = array(0, 1, 2, 3, "default", "hqdefault", "mqdefault", "sddefault", "maxresdefault");
$result = array();
foreach($sizes as $size) {
$result [ $size ] = "{$prefix}//img.youtube.com/vi/{$yt_id}/{$size}.jpg";
}
return array(
'id' => $yt_id,
'link' => "{$prefix}//youtu.be/{$yt_id}",
'sizes' => $result,
);
}
To test this out, loop through a list of IDS.
$yt_videos = array('cV5yxrZpuXg', 'LtBTHqi8Gmw', 'VEAY-nRlP5c', 'IWeasYydL8Y', 'fCVXpcR9KDY');
foreach($yt_videos as $yt_id) {
$ytImages = getYoutTubeImagesFromID($yt_id);
$src = $ytImages[ 'sizes' ][ 'hqdefault' ];
$link = $ytImages[ 'link' ];
echo "<a href=\"{$link}\" target=\"_blank\" ><img src=\"{$src}\" ></a>";
print_r($ytImages);
}
They should each produce size information you can use for your import.
Array
(
[id] => cV5yxrZpuXg
[link] => https://youtu.be/cV5yxrZpuXg
[sizes] => Array
(
[0] => https://img.youtube.com/vi/cV5yxrZpuXg/0.jpg
[1] => https://img.youtube.com/vi/cV5yxrZpuXg/1.jpg
[2] => https://img.youtube.com/vi/cV5yxrZpuXg/2.jpg
[3] => https://img.youtube.com/vi/cV5yxrZpuXg/3.jpg
[default] => https://img.youtube.com/vi/cV5yxrZpuXg/default.jpg
[hqdefault] => https://img.youtube.com/vi/cV5yxrZpuXg/hqdefault.jpg
[mqdefault] => https://img.youtube.com/vi/cV5yxrZpuXg/mqdefault.jpg
[sddefault] => https://img.youtube.com/vi/cV5yxrZpuXg/sddefault.jpg
[maxresdefault] => https://img.youtube.com/vi/cV5yxrZpuXg/maxresdefault.jpg
)
)
STEP 2 | Vimeo
Gather Vimeo video information
function getVimeoImagesFromID($vimeo_id, $prefix = 'https:') {
$vimeo_url = "{$prefix}//vimeo.com/api/v2/video/{$vimeo_id}.php";
if( ! $vimeo_hash = @file_get_contents($vimeo_url)) {
return false;
}
$result = unserialize($vimeo_hash);
return ( count($result) === 1 ) ? array_shift($result) : $result;
}
// your vimeo id
$vimeo_id = 71472926;
// get available information for video
if($vimeo_hash = getVimeoImagesFromID($vimeo_id)) {
// grab the largest image possible
$vimeo_src = $vimeo_hash [ 'thumbnail_large' ];
echo $vimeo_src; // https://i.vimeocdn.com/video/445043557_640.jpg
}
STEP 3 | Sideload Image
media_sideload_image the image from YT to your sever. Be sure to grab the thumbnail_id
from the resulting src
.
// your youtube id
$yt_id = 'cSXlOeKusWs';
// size information for id
$ytImages = getYoutTubeImagesFromID($yt_id);
// max res image
$url = $ytImages[ 'sizes' ][ 'maxresdefault' ];
// sideload image
$src = media_sideload_image(esc_url($url), NULL, '', 'src');
// convert src to id
$thumbnail_id = attachment_url_to_postid($src);
STEP 4 | Attach Post Thumbnail
set_post_thumbnail based on the new thumbnail_id
.
set_post_thumbnail( $post, $thumbnail_id );
_my_theme_post_video
data look like? How will you know it's YouTube vs. Vimeo?