Support

Account

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

Solving

ACF oEmbed – Youtube videos thumbnail set on featured image

  • Hey, guys, you all right?

    Would it be possible or would you be wanting too much, a way to use the thumbnail of a YouTube video as a featured image automatically?

    If this already exists could direct me, my English is not very good to find topics with this type of content, sorry.

    Could someone humbly help me?

  • Hi,

    The easiest way is to create an image custom field and to use it as featured image.

    Or, you can refer to this topic https://support.advancedcustomfields.com/forums/topic/youtube-thumbnail-object-with-oembed-field/

  • I’m guessing you already solved this, but for future reference, this is what I use and it works great. Depending on your own setup, you may want to add more conditional statements to trigger only on certain post types.

    function generate_video_thumbnail($post_id, $post) {
    	$current_post_thumbnail = get_post_thumbnail_id( $post_id );
        if ( '' !== $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);
  • Hi @deset I am trying to set this to work, but I don’t see it. I don’t get errors either. Should I include something in the template too?

    I am using custom post types… I wonder if that is it.

  • 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);
Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.