How to add featured image to WordPress RSS feed

How to add featured image to WordPress RSS feed

Recently I came across a situation when I wanted to add the featured image of the posts to my RSS Feed. By default WordPress does not shows the Featured Image in the RSS Feed. This can be easily achieved by adding a few lines of code in the functions.php file of the theme.

Important: Make sure to add the code to the functions.php file of your theme. Do not add it to the functions.php file in the wp-includes directory.

Here is the code that will add the featured image before the content with a margin-bottom of 10px.

//Function to add featured image in RSS feeds
function featured_image_in_rss($content)
{
	// Global $post variable
	global $post;
	// Check if the post has a featured image
	if (has_post_thumbnail($post->ID))
	{
		$content = get_the_post_thumbnail($post->ID, 'full', array('style' => 'margin-bottom:10px;')) . $content;
	}
    return $content;
}
//Add the filter for RSS feeds Excerpt
add_filter('the_excerpt_rss', 'featured_image_in_rss');
//Add the filter for RSS feed content
add_filter('the_content_feed', 'featured_image_in_rss');

If you want to use different size of image just change the ‘full’ in above code (line 9) to the correct image size name e.g. thumb

	$content = get_the_post_thumbnail($post->ID, 'thumb', array('style' => 'margin-bottom:10px;')) . $content;

To add a custom sized image you can pass the size of to the get_the_post_thumbnail function. e.g. for a image of size 300×169 use the following code:

	$content = get_the_post_thumbnail($post->ID, array(300, 169), array('style' => 'margin-bottom:10px;')) . $content;

If you would like the featured image to be shown below the content, just change the line 9 to

	$content = $content. get_the_post_thumbnail($post->ID, 'full', array('style' => 'margin-bottom:10px;'));

You can add custom code surrounding the image too. e.g. the following code will add the full-sized featured image within a div with class featured_image_post_rss.

//Function to add featured image in RSS feeds
function featured_image_in_rss($content)
{
	// Global $post variable
	global $post;
	// Check if the post has a featured image
	if (has_post_thumbnail($post->ID))
	{
		$content = '<div class="featured_image_post_rss">' . get_the_post_thumbnail($post->ID, 'full') . '</div>' . $content;
	}
    return $content;
}
//Add the filter for RSS feeds Excerpt
add_filter('the_excerpt_rss', 'featured_image_in_rss');
//Add the filter for RSS feed content
add_filter('the_content_feed', 'featured_image_in_rss');
Reference Links:

  1. Hey,

    Thank-you very much for this! — Images are now appearing in my blog’s RSS feed! 🙂

    I do have two questions though.

    1. How am I able to make this image clickable to the post’s URL? (When users click on the image, it takes them to the post).

    2. My WordPress theme gives me a few options before publishing my post. Such as Post, Video, Gallery, and Audio etc.

    How am I able to put the posts that have videos, replacing the featured image for those? — Maybe email would be best for #2. 🙂

    Thank-you for your time, and thank-you very much for this code.

    Riley !

  2. This code crashed my site 🙁

  3. The code just crashed my site too. I can’t get into my admin panel even

  4. Thanks for the post. This works great.

  5. Thanks for sharing this, I’ve been struggling to find a way to add images to my RSS feed for a while now! Where exactly do I add this code though? In my editor I have “themename.functions.php” and “theme functions (functions.php” as options. I want to be sure that I’m adding it to the right one to avoid crashing the site. Also do you suggest placing it in any specific place within the code?

    • I am not sure what theme you are using, but I add this code to the functions.php file inside my wp-content/themes/themename/ directory. This code can be added anywhere in the file. I normally add it at the end of the file.

  6. I used it as well and it gave this error:

    //Function to add featured image in RSS feeds function featured_image_in_rss($content) { // Global $post variable global $post; // Check if the post has a featured image if (has_post_thumbnail($post->ID)) { $content = get_the_post_thumbnail($post->ID, ‘full’, array(‘style’ => ‘margin-bottom:10px;’)) . $content; } return $content; }
    Warning: Cannot modify header information – headers already sent by (output started at /public/sites/www.debbieschrijft.nl/wp-content/themes/adelle-child/functions.php:12) in/public/sites/www.debbieschrijft.nl/wp-includes/pluggable.php on line 896

    What did i do? I copied your example in the first box and pasted it into functions.php in my child theme. Some read more info is also pasted default, i deleted that. After updating the functions.php an error showed. After the first error i deleted this piece of text (“//Add the filter for RSS feeds Excerpt
    add_filter(‘the_excerpt_rss’, ‘featured_image_in_rss’);
    //Add the filter for RSS feed content
    add_filter(‘the_content_feed’, ‘featured_image_in_rss’);”) and saved again. Then the error shown above appeared.

  7. AAAGH. Crashed my site as well!! Doing a restore on my site didn’t work. I’m completely freaking out!!!

  8. Site has been down for an hour. Please take this code down, it is killing websites!

    • Where did you add the code? If you removed the code and your website still didn’t work then there is some other issue not related to this code.

      I have used this code in my website and many other websites and it works fine.

  9. The code works great, just have one question:
    Is there a golden midway between “full” and “thumb”? The one is to big and the other to small

    • You can choose any size of the image that you want. You will have to just change “full” or “thumb” to use an array of the dimensions that you want. I updated my post to add an example to use an image size of 300×169.

  10. i put it on the very bottom of functions of the functions.php then my website is crashed.. where did i should put this code?? thanks Mark

    • Which file did you put the code in? This code should be added to the functions.php file of your theme and not the one in wp-includes.

      Also, what were the errors that you were seeing when your website crashed. Anything from error logs will be useful.

  11. Does it make it show in bloglovin? I dont know which size to use to show there… I just recently started :S
    The code is just fine, if you crashed it try to go to your cpanel and remove there de code from the file and save and then tou can enter again in the wordpress! I mean thats what I did anyway , because i found one site that told me to do something similar and it crashed dont know why but this one is working!

Leave a Reply