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!

Related posts:

  1. Custom Post Type UI Plugin for WordPress
  2. How To: Remove Default Profile Fields in WordPress
  3. How to: Update FriendFeed using ASP
  4. How To: Save Taxonomy Meta Data as an Options Array in WordPress
  5. How To: Add A Link to the WordPress Multisite Network Admin Sites List
Enjoy this post? Be sure to subscribe to my RSS feed and my WordPress Tips and Tricks Newsletter! Also check out my new book: Professional WordPress

Comments

  1. Max says:

    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. Brad says:

    @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. James says:

    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. Ted Mann says:

    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?

    • Brad says:

      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

      • erica says:

        Brad,

        Did you ever get around to posting the code (to add an image from your post or the 1st image in post) to the RSS feed

  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?

  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. erica says:

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

  10. Uli says:

    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. Ranui says:

    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. ryan says:

    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. Brett says:

    You da man, Brad! Muchas Gracias!

  14. Chad says:

    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. E says:

    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!

Speak Your Mind

*