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. How To: Create Backdoor Admin Access in WordPress
  2. How To: Redirect A User After Logging Into WordPress
  3. How To: Remove Default Profile Fields in WordPress
  4. How To: Hide an Admin Menu in WordPress
  5. How To: Load User Info Using the Admin Email in WordPress
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

About Brad
I am the CEO and Co-Founder of WebDevStudios.com, a co-host on the SitePoint Podcast, and the co-author of 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

  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…

Speak Your Mind

*