WordPress: Add featured image to RSS feed

I’m a firm believer in minimising the amount of plugins a WordPress site uses and instead making code changes to get what you need. This removes the overall bloat of a site and makes things far easier to manage. From time to time I come across useful plugins, but instead of using them I work out the quickest way to achieve the same goal by coding it myself (for example). Here’s a simple function I use to show a post’s featured image in the RSS feed (something WordPress does not do by default):

function featured_image_in_feed( $content ) {
    global $post;
    if( is_feed() ) {
        if ( has_post_thumbnail( $post->ID ) ){
            $output = get_the_post_thumbnail( $post->ID, 'medium', array( 'style' => 'float:right; margin:0 0 10px 10px;' ) );
            $content = $output . $content;
        }
    }
    return $content;
}
add_filter( 'the_content', 'featured_image_in_feed' );

Just paste that code into your theme’s functions.php file and you’re good to go. You can obviously modify the style attribute to have it display however you wish, as well as the image size (set to ‘medium’ in this example) – the options for this are: thumb, thumbnail, medium, large & post-thumbnail.

Tags: , , , , ,

8 Responses to “WordPress: Add featured image to RSS feed”

  1. Gary September 20, 2012 at 5:59 pm #

    Hi,
    Just used your snippet on a clients website. Worked a treat. Thank you.

  2. Javi October 13, 2012 at 12:24 pm #

    Hi,
    I did what you said in my wordpress blog and it did not work. There are lots of people looking for this feature and I do not want to install a plugin at all.

    I have two function files functions.php and theme-functions.php and I tried already in both.

    I am not seeing the images in your feed neither… Hope you can enlighten me

    Thanks

    • Hugh Lashbrooke October 24, 2012 at 8:08 am #

      Hi Javi,

      The code itself should work fine if you paste it into your theme’s functions.php file in the root folder for the theme. If it’s not working for you then make sure you are setting your featured image correctly (using the WP featured image and not your theme’s custom image setting).

      Since changing some things on my site I have removed the images from my feed – I’ll put them back sometime, but for now you won’t see them there.

      Hugh

  3. AR May 8, 2013 at 1:40 am #

    I have been trying to give an image to RSS I include on my WP blog. However, it always results in failure. your recent writing I am reading now works well, unfortunately I am not so familiar with coding or something similar. could you please provide me with something simpler instead?

    • Hugh Lashbrooke May 8, 2013 at 7:24 am #

      I have tested this code again in the latest version of WordPress and it works correctly as it is now – I can’t really provide you with anything simpler as this is about as simple as you can get for this kind of thing.

  4. Victor Lava May 8, 2013 at 5:04 pm #

    Worked like a charm. Thanks for a code share! :)

  5. Camp Trend May 23, 2013 at 7:08 pm #

    Thanks so much, works perfect!!

Leave a Reply