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.
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
11 responses to “Set WordPress Featured Image Automatically”
Nice and clean solution! Bookmarked.
[…] Set WordPress Featured Image Automatically – WPengineer.com Posted by Jerry DeFoe on Thursday, June 21, 2012 […]
[…] 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 …Find out more from WPengineer.com […]
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
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
now i am work on post image. this very help form me. Thanks
can you give me information about image crop in featured image ?
Frank, Does this code set the First image in the post as featured image or some other default image?
@Chris: yes, use the first image in the Gallery to an post, see this on the tab Gallery on the mediathek thickbox.
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!
Well this workaround will definitely help me to reduce my headache to add feature image everytime with new post.
Thanks for the workaround.
[…] Set WordPress Featured Image Automatically Sometimes we can be forgetful when posting and absent-mindedly add a post to our homepage without a featured image. While this may not be something you want to add to a theme you are developing that others will use, it could be pretty useful for your own personal site. The plugin from WPEngineer simply takes an image loaded in your post and makes that the default featured image. Could be handy in some situations. […]