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.
Hi,
Just used your snippet on a clients website. Worked a treat. Thank you.
I’m glad it helped
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
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
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?
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.
Worked like a charm. Thanks for a code share!
Thanks so much, works perfect!!