Automatically Set Featured Images


I have a love/hate relationship with featured images on WordPress. I love them but I often forget to set them. Usually I am just forgetful and end up publishing posts before I have the feature image set. That is ok for me who knows what I have done and can go back and set the featured image. Not long ago I had a client where the featured image was an essential part of every post and they were regularly forgetting to set the featured image and well they were getting tired of emailing me to fix their posts for them. With that in mind here are some ways to fix your forgetfulness and automatically have a featured image set for you.

Require Featured Image This plugin does not actually set the featured image for you but it does give you some strong reminders that you need to set a featured image and if for some reason you still don’t remember, well it hooks into the publishing button to disable it until you do add a featured image.

Easy Add Thumbnail

No picture to show you how this plugin works since it is so simple. For older posts the featured image is set when the theme first calls the post. If no featured image is set the plugin will set it to the first image it finds in the post. For new posts it does the exact same thing when you publish the post.

Auto Post Thumbnail

Auto Post Thumbnail PRO The Auto Post Thumbnail plugin is similar to the Easy Add Thumbnail plugin. It tries to generate a featured image from the first image in a post or any custom post type. If the first image doesn’t work it will automatically go to the next image and so on until a featured image is added. This plugin also has a pro version available on CodeCanyon that also offers the ability to add featured images from external images, imported RSS feeds, for old/legacy posts via a single click and auto thumbnail generation for popular video sharing services like YouTube, Vimeo, DailyMotion and more. If the free version works out for you the Pro version is only $7.00 so it might be worth looking into.

Create Your Own

Another option is to create your own plugin or add some code to your theme functions.php file to do this for you automatically. This snippet of code I found on WPMU.org it is actually a modified version from WPForce. I will share the snippet here and you can try it out in your themes functions.php file.

[php]function autoset_featured() { global $post; $already_has_thumb = has_post_thumbnail($post->ID); if (!$already_has_thumb) { $attached_image = get_children( “post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1” ); if ($attached_image) { foreach ($attached_image as $attachment_id => $attachment) { set_post_thumbnail($post->ID, $attachment_id); } } } } //end function add_action(‘the_post’, ‘autoset_featured’); add_action(‘save_post’, ‘autoset_featured’); add_action(‘draft_to_publish’, ‘autoset_featured’); add_action(‘new_to_publish’, ‘autoset_featured’); add_action(‘pending_to_publish’, ‘autoset_featured’); add_action(‘future_to_publish’, ‘autoset_featured’);[/php]

The short version of what the code does. It checks to see if a post has a featured image and if it does not sets it to the first image in the post.

Of course if you are not forgetful you might not have to worry about automatically setting the featured image. For my client I went with adding the snippet of code to their functions.php file and it has been working great since I did it. Not a single email from them because of a missed featured image.

Categories: snippet   wordpress