Adding video content is one of the best ways to make your WordPress posts more engaging—but uploading thumbnails manually for every post can slow you down. This guide walks through how to create a custom ACF (Advanced Custom Fields) field for video URLs that automatically sets the featured image using the video’s thumbnail.
Issue background
A WordPress client wanted to streamline their publishing process by adding a custom “Video URL” field to posts. The goal was simple: when a YouTube or Vimeo URL was added, the featured image should automatically update to that video’s thumbnail. This feature helps content managers maintain consistent visuals across post grids without manually uploading images.
Diagnosis
The challenge centered on dynamically retrieving and assigning the thumbnail image from an external video URL. YouTube provides a predictable image URL format, making it easy to generate a thumbnail automatically. Vimeo and raw video files (.mp4) required additional logic, as they do not expose image URLs in the same way.
Resolution steps
1. Install and activate the ACF plugin
If you haven’t already, install Advanced Custom Fields to create custom input fields.
2. Create a new field group
- In your WordPress dashboard, go to Custom Fields → Add New.
- Add a field called Video URL and set the type to URL.
- Under Location Rules, assign the field group to Posts (or any desired post type).
3. Add logic to automatically set the featured image
Edit your theme’s functions.php file and add this snippet:
add_action('save_post', 'set_featured_image_from_video');
function set_featured_image_from_video($post_id) {
// Avoid running during autosave
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;
$video_url = get_field('video_url', $post_id);
if (!$video_url) return;
// Only proceed if there’s no featured image
if (!has_post_thumbnail($post_id)) {
if (strpos($video_url, 'youtube.com') !== false) {
// Extract the YouTube video ID
preg_match('/v=([^&]+)/', $video_url, $matches);
if (isset($matches[1])) {
$video_id = $matches[1];
$thumbnail_url = "https://img.youtube.com/vi/{$video_id}/0.jpg";
// Download and set image
media_sideload_image($thumbnail_url, $post_id, null, 'id');
}
}
// Add optional Vimeo support here
}
}
This function checks for a YouTube video URL and automatically assigns the corresponding thumbnail as the featured image when the post is saved.
4. Test your implementation
- Create a new post and paste in a YouTube video URL.
- Publish or update the post.
- The featured image should now display the video thumbnail.
5. Extend support for other video types
- Vimeo: Use Vimeo’s API to retrieve thumbnail URLs.
- .mp4 uploads: Since raw video files don’t generate thumbnails, instruct users to upload a custom image manually for those posts.
Final outcome
This setup saves valuable time for content creators and ensures visual consistency across video posts. YouTube URLs automatically pull a clean, high-resolution thumbnail—no manual uploads required.
For more complex setups (like integrating Vimeo, or automating thumbnail retrieval for custom video hosts), the Freshy development team can help tailor the functionality to your site’s needs.
👉 Contact Freshy today to get expert WordPress support.