Home › Forums › General Issues › ACF oEmbed – Youtube videos thumbnail set on featured image › Reply To: ACF oEmbed – Youtube videos thumbnail set on featured image
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);
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.