Is there a plugin for this? Has anyone done it before?


I use the Auto Post Thumbnail plugin for doing just that on this site and it works fine. First image in a post becomes the featured image every time.

  • doesnt seem to work with inline images on current wp 3.8.1
    – Toskan
    Feb 25 '14 at 14:56
  • @Toskan I just uploaded a new image to that very example site, on WP 3.8.1, and it worked fine. Can you maybe be a little more precise about what you're doing and how it's not working? (Bear in mind it won't automatically work on existing content -- there's a "Generate Thumbnails" button on the plugin's settings screen that will generate featured images for existing posts...) Feb 25 '14 at 20:45
  • yes I did that. Are we talking about the same though, are you considering an inline image to be as well something like this <img src="../pic/a.jpg"> ?
    – Toskan
    Feb 26 '14 at 10:45
  • or in other words, and inline image is certainly NOT an attachment of a post for me.
    – Toskan
    Feb 26 '14 at 12:48
  • @Toskan I just tried it with a random image from my Flickr stream, just pasted into a new Post as <img src="http://farm4.staticflickr.com/3822/12722196853_252881b4b6_z.jpg">. It ended up as the Featured Image just fine. Don't know what you're doing differently, but I can't get it not to work. Sorry! The documentation says "The first image in your post is used as Post Thumbnail. This is irrespective of whether the image is an uploaded image or inserted through URL", and that's working for me. Feb 26 '14 at 15:34

You may find the answer in this tutorial : How to Set a Default Fallback Image for WordPress Post Thumbnails

Add this in functions.php in your theme folder :

add_theme_support( 'post-thumbnails' );

function myprefix_main_image() {
    $attachments = get_children( 'post_parent='.$post->ID.'&post_type=attachment&post_mime_type=image&order=desc' );
    if( $attachments ) {
        $keys = array_reverse( $attachments );
        set_post_thumbnail( $post->ID, $keys[0]->ID );

And this in your template, where you want to show the post image :

<?php if ( (function_exists( 'has_post_thumbnail') ) && ( has_post_thumbnail() ) ) {
  echo get_the_post_thumbnail( $post->ID );
} else {
   echo get_the_post_thumbnail( $post->ID );
} ?>

EDIT : much better, thanks Chip Bennett

  • Two comments: 1) function main_image() needs to be prefixed appropriately; the name is too generic. 2) The function would be so much better if it incorporated a set_post_thumbnail() on whatever image it returns. Jul 22 '11 at 13:39
  • You're right Chip, the code was ugly, I had copy-pasted it from the tutorial. I updated it, now it sets the first content image as a post_thumbnail.
    – mike23
    Jul 22 '11 at 14:02
  • Just looking at the code, do you need to return the Post Thumbnail when you're done? Jul 22 '11 at 14:08
  • Updated, the thumbnail is echoed after our function is called.
    – mike23
    Jul 22 '11 at 14:22
  • $post->ID should be get_the_ID() inside the function. Otherwise you'll set every post's thumbnail to the same image! You could also incorporate your has_post_thumbnail() logic into one function that just returns the thumbnail no matter what, which would make your template cleaner.
    – Nick
    Mar 28 '15 at 1:56

Get The Image is great plugin for related functionality. You will need to configure funciton call a bit, but it will be able both to scan for image and save it into thumbnail for you. Was writing quickly and messed up a bit. What it can do is save found image into custom field (which might or might not be what you want). If you want to actually make featured image out of it - that is going to be considerably more complex.

  • i think i had some code once that scanned through the post and picked up any images, but get the image does more and is neater. if i recall, it doesn't scan by default, but it is one of the many options available. Jul 22 '11 at 15:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.