How To: Add a Post Thumbnail to an RSS Feed in WordPress

Have you ever needed to add the WordPress post thumbnail to an existing RSS feed? The below code will add a new element named <thumb> to your RSS feed. This element will contain a link to the post thumbnail as set in WordPress:

function ThumbRSS() {
	global $post;
	if ( has_post_thumbnail( $post->ID ) ) { 
		$thumbpic = get_the_post_thumbnail( $post->ID, 'thumbnail' ); 
	}
	
	echo '<thumb>'.$thumbpic.'</thumb>';
}

add_filter('rss_item', 'ThumbRSS');

Keep in mind using this technique will devalidate your RSS feed as the <thumb> element is not a part of the RSS specification. An alternate approach is to attach the post thumbnail to the beginning of your post content in your RSS feed. Below is an example using this method:

function ThumbRSS($content) {
   global $post;
   if ( has_post_thumbnail( $post->ID ) ){
       $content = '<p>' . get_the_post_thumbnail( $post->ID, 'thumbnail' ) . '</p>' . $content;
   }
   return $content;
}

add_filter('the_excerpt_rss', 'ThumbRSS');
add_filter('the_content_feed', 'ThumbRSS');

Just drop either code example in your themes functions.php file for this to work. Pretty easy huh? Now you can easily include post thumbnails in your WordPress RSS feeds!

Enjoy this post? Be sure to subscribe to my RSS feed and my WordPress Tips and Tricks Newsletter! Also check out my WordPress books: Professional WordPress Third Edition & Professional WordPress Plugin Development

Comments

  1. Brad, surfing on the net I found your very smart sol to add thumbs in RSS Feed without any plugin… (i tried the 2nd method) But in my site all the thumbs are created picking up the 1st image in the post, so I think that your code works if you define when you edit a new post an image as thumb… As I have around 400 posts, would like to not have to add thumbs in all of them. Do you think there’s any sol to do it, again without installing any plugin? Thx!

  2. @Max – correct this only works if you have thumbnails set. I have code I’ll clean up and post later that shows how to grab and image from your post content or grab the first attachment of a post.

  3. Brad, thanks for your post. I think it is the first step to solving a problem I have. I am trying to syndicate feeds from multiple WordPress sites to a single portal, using the thumbnails as thumbnails from each of the sites as they are pulled from rss. The site doing the aggregation gets the thumbnails with your code, but does not recognize them as thumbnails in the sense that wordpress does. Any ideas on how to solve this one?

  4. Took me a while sifting through google but so glad I found this. By far the best solution I’ve seen, do you know how many people are trying to use a multitude of plugins to do what you just did in a few lines?? Thanks a ton!

  5. Thank you for sharing this Brad. I really wanted to include the thumbnails in the RSS feed without using any plugin. This really helped 😉

  6. Brad, do you know if there’s a way to grab a post image, even if the post thumbnail hasn’t been set? Say, grabbing the first image attachment for the post, and using that for the RSS image?

    • Yeah I have code to do exactly that. We have our own custom built function that checks for a featured image, if it doesn’t exist checks for any images uploaded on the post, and if that doesn’t exist it looks for any images embedded in the post, and if that still doesn’t exist it falls back to a default image.

      I’ll write a blog post detailing how I do this soon

  7. Did this get resolved. i have images in my RSS feed, but they are only 150×150. How can I increase the size of feed images to full size?

    Is there a way?

    • Very simple actually, go to Settings (in the admin area), then Media, in your Media Settings change your Thumbnail, Medium and large to any sizes you want. If that’s not enough you could always add other custom sizes through your function.php file. Let me know and I will post something here for you to do that.

  8. Very useful post. I also found that using the WP RSS Images plugin also worked. Like above, is there a way to change the image size in the feed? I’ve changed the Media –> thumbnail size from the WordPress admin, but it isn’t making a difference.

    Stumped…

  9. How do I add a the posts Featured Image to my RSS Feed

    • You need to add this little script to you function.php file. Make a backup of your original file before changing anything in the function.php file. [WARNING] You could damage your site/blog if not done correctly:

      function featuredtoRSS($content) {
      global $post;
      if ( has_post_thumbnail( $post->ID ) ){
      $content = ” . get_the_post_thumbnail( $post->ID, ‘thumbnail’, array( ‘style’ => ‘float:left; margin:0 15px 15px 0;’ ) ) . ” . $content;
      }
      return $content;
      }

      add_filter(‘the_excerpt_rss’, ‘featuredtoRSS’);
      add_filter(‘the_content_feed’, ‘featuredtoRSS’);

      Here is the original link to the script, by Alex Joy : http://webdesignzo.com/show-featured-images-in-rss-feed-feedburner/

  10. Hey,
    I am only using summary feeds (not full feeds). When I include your modifications to the functions.php file in my theme folder, my feed now goes to full feed: it shows the thumbnail plus the whole post (that includes a larger image version of the thumbnail).

    See members.skinnychef.com/feed

    What triggers the switch to full feed (despite having set the “Summary only” option in the wordpress admin settings)?

  11. I got this error: Parse error: syntax error, unexpected ‘}’ in /home/thefashi/public_html/wp-content/themes/The_Fashion_Tank20/functions.php on line 289
    thanks, i have no idea what to do now.

  12. nice thanks for sharing wonderful post.

    Here I have another tutorial on how to make your first image post to be a thumbnail automatically Without Custom field and Featured image and without taking your time editing cropping thumbnail of your post.

    Automatic WordPress Thumbnail Without Custom field and Featured image

  13. You da man, Brad! Muchas Gracias!

  14. What is the best way to align the thumbnail image so it’s float to the left of the text? It seems like the style tag automatically gets stripped out.

  15. This is a great post. Quick question. In this code, the thumbnail image is above the post excerpt. Is there a way to align them side by side in this code?

    The goal is to have the thumbnail on the left and the post excerpt to the right (instead of below).

    Thanks so much for your help.

  16. The code looks very simple 🙂 How can I modify it to display custom posts thumbs? Thanks in advance!

  17. Been looking for such feed plugin! Thanks

  18. REally very good post . it’s good way to add Thumbs to Rss feed

  19. yes i am looking for how to add thumbnail to rss feed using external image, like from flickr, amazon, etc…can you show me.

    Thanks

  20. Hmmmm. I added this at bottom of functions.php, and got this text showing up in the header of the site:
    function ThumbRSS($content) { global $post; if ( has_post_thumbnail( $post->ID ) ){ $content = ‘

    ‘ . get_the_post_thumbnail( $post->ID, ‘thumbnail’ ) . ‘
    ‘ . $content; } return $content; } add_filter(‘the_excerpt_rss’, ‘ThumbRSS’); add_filter(‘the_content_feed’, ‘ThumbRSS’);
    Warning: session_start() [function.session-start]: Cannot send session cache limiter – headers already sent (output started at /home/landin6/public_html/wp-content/themes/cityguide/functions.php:38) in /home/landin6/public_html/wp-content/plugins/si-contact-form/si-contact-form.php on line 1997