Skip to content

Instantly share code, notes, and snippets.

@tilap
Last active July 23, 2023 09:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tilap/2259b6417d578f1145da to your computer and use it in GitHub Desktop.
Save tilap/2259b6417d578f1145da to your computer and use it in GitHub Desktop.
Wordpress: How to automatically attache youtube/vimeo thumbnail as a post feature image from a post meta key
function insertThumbnail($data, $postArr) {
if(!isset($postArr['ID'])) {
return $data;
}
$postId = $postArr['ID'];
$metaKey = 'you-post-meta-video-key'; // the custom meta key
// If video and video format
if(isset($postArr[$metaKey]) && ''!==$postArr[$metaKey] ) {
$imageThumbnailUrl = ''; // the image remote url if any
$videoUrl = $postArr[$metaKey]; // the post meta value givin' us the video url
// Vimeo
if(false!==strpos($videoUrl, 'vimeo')) {
if(preg_match('/\/([\d]+)$/', $videoUrl, $reg)) {
$vimeoId = $reg[1];
// See http://stackoverflow.com/questions/1361149/get-img-thumbnails-from-vimeo
$infoUrl = sprintf('http://vimeo.com/api/v2/video/%1$s.%2$s', $vimeoId, 'json');
$ch = curl_init ($infoUrl);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
$vimeoRawData=curl_exec($ch);
curl_close ($ch);
$vimeoData = @json_decode($vimeoRawData);
if(false!==$vimeoData) {
$imageThumbnailUrl = $vimeoData[0]->thumbnail_large;
}
}
}
// Youtube
else if(false!==strpos($videoUrl, 'youtube')) {
if(preg_match('/.*v=([^&]+).*$/', $videoUrl, $reg)) {
$youtubeId = $reg[1];
$imageThumbnailUrl = sprintf('http://img.youtube.com/vi/%1$s/hqdefault.jpg', $youtubeId);
}
}
// If any thumbnail, import the file locally and attach it to the post
if(''!==$imageThumbnailUrl) {
$filename = 'tripmemore-' . $postArr['post_name'].'-' .md5($imageThumbnailUrl). '.jpg';
$uploadDirInfo = wp_upload_dir(); // @see http://codex.wordpress.org/Function_Reference/wp_upload_dir
$targetFilePath = $uploadDirInfo['basedir'] . '/' . $filename;
$targetFileUrl = $uploadDirInfo['baseurl'] . '/' . $filename;
// Copy image in local
$ch = curl_init ($imageThumbnailUrl);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
$raw=curl_exec($ch);
curl_close ($ch);
$fp = fopen($targetFilePath,'x');
fwrite($fp, $raw);
fclose($fp);
// Create the attachment
$wp_filetype = wp_check_filetype($targetFilePath, null );
$attachment = array(
'guid' => $filename,
'post_mime_type' => $wp_filetype['type'],
'post_title' => sanitize_file_name($filename),
'post_content' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment( $attachment, $filename);
// Set the thumbnail as feature image
require_once(ABSPATH . 'wp-admin/includes/image.php');
$attach_data = wp_generate_attachment_metadata( $attach_id, $targetFilePath );
$res = wp_update_attachment_metadata( $attach_id, $attach_data );
$res = set_post_thumbnail( $postId, $attach_id );
// Update the post meta
update_post_meta($postId, $metaKey, $postArr[$metaKey]);
}
}
return $data;
}
add_filter( 'wp_insert_post_data', 'insertThumbnail', '95', 2 ); // @see http://codex.wordpress.org/Plugin_API/Filter_Reference/wp_insert_post_data
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment