Support

Account

Home Forums General Issues ACF oEmbed – Youtube videos thumbnail set on featured image Reply To: ACF oEmbed – Youtube videos thumbnail set on featured image

  • Hi @deset!
    Thanks a lot for this great solution! It works for me but contains one missprint. If I understood the get_post_thumbnail_id() sourcecode this function returns int and can’t return empty string.
    I’ve changed
    if ( '' !== $current_post_thumbnail )
    to
    if ( 0 !== $current_post_thumbnail )
    And it works!

    @marbaque this may be the cause for the problem you faced with, please check this

    full @deset ‘s code with this fix:

    function generate_video_thumbnail($post_id, $post) {
    	$current_post_thumbnail = get_post_thumbnail_id( $post_id );
        if ( 0 !== $current_post_thumbnail ) {
            return;
        }
    	
    	$url = get_field('video_embed', $post_id, false);
    	$oembed = _wp_oembed_get_object();
    	$provider = $oembed->get_provider($url);
    	$oembed_data = $oembed->fetch( $provider, $url );
    	$thumbnail = $oembed_data->thumbnail_url;
    	$title = get_the_title($post_id);
    	
    	if($url) {
    		$image = media_sideload_image( $thumbnail, $post_id, $title, 'id' );
    		set_post_thumbnail( $post_id, $image );
    	}
    }
    add_action('save_post', 'generate_video_thumbnail', 12, 3);