Set Automatically Thumbnail of Image, Embed YouTube Video without Plugins

Many type of plugins for automatically set thumbnail/featured_image of Image and YouTube video. But plugins have extra unused code. So i have one code for set automatically thumbnail when you upload image, or if you embed YouTube video.

How to Do

Paste given code on functions.php then SAVE.

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(' ', '–', ',', '|' ,'.'), '-', $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');

This code tested on custom WordPress theme.

FAQ

It it save for website?
Yes! use this code without any worries.
Can use this code any theme?
Sorry, i don’t know this code work to any theme, but i use this code on custom theme and this code works perfectly on custom theme.