Use this code for set automatically thumbnail without plugins –
//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('/<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');