Set WordPress Featured Image Automatically

Anyone who works in his WordPress blog with a picture and always wants or need to put one on his post - depending on the layout implementation, might like this small Plugin, which can do the setting automatically.
If not a Featured Image is set it grabs the first image that was uploaded to the article.

Set featured image on WordPress

As another alternative, you could define another image as default image, as a fallback solution if no image was uploaded. Every time the post is saved or updated, the image will be checked and if necessary set as featured image.
Further solutions to work with images and files of the media library can be found in another post. If you want to controll the post view, then you have to add some line of code to it, see our article "Display Post Thumbnail Also In Edit Post and Page Overview". Alternatively I extended the Plugin and you can grab it from Github.

<?php
/**
 * Plugin Name: Set featured image
 * Plugin URI:  http://bueltge.de
 * Description: Set featureed image automaticly on save post/page
 * Version:     1.0.0
 * Author:      Frank Bültge
 * Author URI:  http://bueltge.de
 * License:     GPLv3
 */

// This file is not called by WordPress. We don't like that.
! defined( 'ABSPATH' ) and exit;

if ( ! function_exists( 'fb_set_featured_image' ) ) {
	
	add_action( 'save_post', 'fb_set_featured_image' );
	function fb_set_featured_image() {
			
			if ( ! isset( $GLOBALS['post']->ID ) )
				return NULL;
				
			if ( has_post_thumbnail( get_the_ID() ) )
				return NULL;
			
			$args = array(
				'numberposts'    => 1,
				'order'          => 'ASC', // DESC for the last image
				'post_mime_type' => 'image',
				'post_parent'    => get_the_ID(),
				'post_status'    => NULL,
				'post_type'      => 'attachment'
			);
			
			$attached_image = get_children( $args );
			if ( $attached_image ) {
				foreach ( $attached_image as $attachment_id => $attachment )
					set_post_thumbnail( get_the_ID(), $attachment_id );
			}
			
	}
	
}

Comments are closed.

11 comments

  1. Daan Kortenbach

    Nice and clean solution! Bookmarked.

  2. Cathy Dutton

    Hi Frank

    Thanks for posting this easy to follow tutorial, I think it will prove very useful when working on large content managed websites in wordpress. It will also help customers manage their websites much more easily and avoid page layout differences if no featured image has been set.

    Cathy

  3. kevin Chard

    I may want to confirm the size of the image is ok first depending on how I used featured images, however very interesting solution. Nice article

  4. sanjay modi

    now i am work on post image. this very help form me. Thanks
    can you give me information about image crop in featured image ?

  5. Chris Lynn

    Frank, Does this code set the First image in the post as featured image or some other default image?

  6. Frank

    @Chris: yes, use the first image in the Gallery to an post, see this on the tab Gallery on the mediathek thickbox.

  7. Topher

    I'm using this with the Complete Gallery Manager for WordPress.

    http://codecanyon.net/item/complete-gallery-manager-for-wordpress/2418606

    It doesn't have images in the content, but rather a series of them stored in an option, serialized.

    I also can't make the gallery work on multi-site without a featured image, so I tweaked the above code like this:

    $meta_values = get_post_custom();

    $image_data = get_object_vars(json_decode(urldecode($meta_values['cgm-gallery-data'][0])));

    $first_image_id = $image_data[0]->postid;

    set_post_thumbnail( get_the_ID(), $first_image_id );

    And it worked perfectly. It simply gets the first image in the gallery and sets it as featured.

    Thanks for the pointer, this saved me lots of work!

  8. Dheeraj

    Well this workaround will definitely help me to reduce my headache to add feature image everytime with new post.
    Thanks for the workaround.

3 pingbacks

  1. Set WordPress Featured Image Automatically - WPengineer.com
  2. Trendy and User-friendly Website Chat Tool for Wordpress Bloggers … | Open Knowledge
  3. This Week in WordPress: July 30, 2012 | Max Foundry