How to Set Automatically YouTube Video’s Thumbnail | WordPress

Set Automatically YouTube Embed Video’s Thumbnail without using any type of plugins when you embed YouTube videos using <iframe> tag or add data-id.

How to Do

  • Open functions.php file.
  • Then paste given code and SAVE.

code

function set_automatically_featured_image_from_embed_YT_videos_thekroyaard($post_ID) {
$post_content = get_post_field('post_content', $post_ID);
if (preg_match('/<iframe.*?src="(?:https?:\/\/)?(?:www\.)?youtube\.com\/embed\/([a-zA-Z0-9_-]+).*?<\/iframe>/i', $post_content, $matches)) {
$video_id = $matches[1];
$thumbnail_url = "https://img.youtube.com/vi/{$video_id}/hqdefault.jpg";
$image_url = media_sideload_image($thumbnail_url, $post_ID, 'Featured Image for Video');
if (!is_wp_error($image_url)) {
$image_id = attachment_url_to_postid($image_url);
set_post_thumbnail($post_ID, $image_id);
}
} elseif (preg_match('/<div.*class="youtube-player".*data-id="([^"]*)/i', $post_content, $matches)) {
$video_id = $matches[1];
$thumbnail_url = "https://img.youtube.com/vi/{$video_id}/hqdefault.jpg";
$image_url = media_sideload_image($thumbnail_url, $post_ID, 'Featured Image for Video');
if (!is_wp_error($image_url)) {
$image_id = attachment_url_to_postid($image_url);
set_post_thumbnail($post_ID, $image_id);
}
}
}
add_action('publish_post', 'set_automatically_featured_image_from_embed_YT_videos_thekroyaard');

function first_image_thumbnail_automatic_thekroyaard($post_id){
$medias = get_attached_media( 'image', $post_id );
if(!has_post_thumbnail($post_id)) {
foreach ($medias as $media) {
set_post_thumbnail($post_id, $media->ID);
break;
}
}
}
add_action('save_post', 'first_image_thumbnail_automatic_thekroyaard');

or you want to automatically ALT name on youTube’s thumbnail so paste this another full code .

//Automatic Thumbnail when embed YT videos lite or standard version
function set_automatically_featured_image_from_embed_YT_videos_thekroyaard($post_ID) {
if (!has_post_thumbnail()) {
$post_content = get_post_field('post_content', $post_ID);
$alt_name_make = get_post( $post_ID )->post_title;
$auto_alt_id = get_the_ID();
$alt_name_YT = wp_trim_words( $alt_name_make, 5, $more = __( ' '.' Thumbnail '. $auto_alt_id ) );
$alt_name_YT = str_replace(array(' ', '&#8211;', ',', '|' ,'.'), '-', $alt_name_YT);
$alt_name_YT = preg_replace('/[^A-Za-z0-9\-]/', '', $alt_name_YT);
$alt_name_YT = preg_replace('/-+/', '-', $alt_name_YT);
if ( preg_match( '/<img.*?src=[\'"](.*?)[\'"]/i', $post_content, $matches ) ) {
$image_url = $matches[1];
$image_id = attachment_url_to_postid($image_url);
if ($image_id) {
set_post_thumbnail($post_id, $image_id);
update_post_meta($post_id, '_thumbnail_size', 'small-thumbnail');
}
} elseif (preg_match('/<iframe.*?src="(?:https?:\/\/)?(?:www\.)?youtube\.com\/embed\/([a-zA-Z0-9_-]+).*?<\/iframe>/i', $post_content, $matches)) {
$video_id = $matches[1];
$thumbnail_url = 'https://img.youtube.com/vi/' . $video_id . '/hqdefault.jpg';
$thumbnail_data = file_get_contents($thumbnail_url);
if ($thumbnail_data) {
$filename = $video_id . '.jpg';
$upload_dir = wp_upload_dir();
$thumbnail_path = $upload_dir['path'] . '/' . $filename;
file_put_contents($thumbnail_path, $thumbnail_data);
$attachment_data = array(
'post_mime_type' => 'image/jpeg',
'post_title' => sanitize_file_name($filename),
'post_content' => '',
'post_status' => 'inherit'
);
$attachment_id = wp_insert_attachment($attachment_data, $thumbnail_path, $post_id);
$attachment_metadata = wp_generate_attachment_metadata($attachment_id, $thumbnail_path);
wp_update_attachment_metadata($attachment_id, $attachment_metadata);
set_post_thumbnail($post_id, $attachment_id);
update_post_meta($attachment_id, '_wp_attachment_image_alt', $alt_name_YT);
}
} elseif (preg_match('/<div.*class="youtube-player".*data-id="([^"]*)/i', $post_content, $matches)) {
$video_id = $matches[1];
$thumbnail_url = 'https://img.youtube.com/vi/' . $video_id . '/hqdefault.jpg';
$thumbnail_data = file_get_contents($thumbnail_url);
if ($thumbnail_data) {
$filename = $video_id . '.jpg';
$upload_dir = wp_upload_dir();
$thumbnail_path = $upload_dir['path'] . '/' . $filename;
file_put_contents($thumbnail_path, $thumbnail_data);
$attachment_data = array(
'post_mime_type' => 'image/jpeg',
'post_title' => sanitize_file_name($filename),
'post_content' => '',
'post_status' => 'inherit'
);
$attachment_id = wp_insert_attachment($attachment_data, $thumbnail_path, $post_id);
$attachment_metadata = wp_generate_attachment_metadata($attachment_id, $thumbnail_path);
wp_update_attachment_metadata($attachment_id, $attachment_metadata);
set_post_thumbnail($post_id, $attachment_id);
update_post_meta($attachment_id, '_wp_attachment_image_alt', $alt_name_YT);
}
}
}
}
add_action('publish_post', 'set_automatically_featured_image_from_embed_YT_videos_thekroyaard');

Hope this code will worked. Thanks for visiting here.

FAQ

Can use this code on custom theme?
Yes! you can use this code on custom theme.