Hugh Lashbrooke

Product manager by day, tabletop game designer by night.

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. 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):


View this gist on GitHub

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.

54 responses to “WordPress: Add featured image to RSS feed”

  1. Gary Avatar
    Gary

    Hi,
    Just used your snippet on a clients website. Worked a treat. Thank you.

    1. Hugh Lashbrooke Avatar
      Hugh Lashbrooke

      I’m glad it helped 🙂

    2. Suresh Khanal Avatar
      Suresh Khanal

      It was really a headache to see the RSS-to-email campaigns without no images because the RSS feed did not have the necessary tag to pull images from. Anyway, changed the functions.php file and now MailChimp can pull the required components and now the emails look beautiful.

      However, still the images in RSS feed look really big and wish to resize them to a suitable size. Needs to search more and find a solution for this.

  2. Javi Avatar
    Javi

    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

    1. Hugh Lashbrooke Avatar
      Hugh Lashbrooke

      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

  3. AR Avatar
    AR

    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?

    1. Hugh Lashbrooke Avatar
      Hugh Lashbrooke

      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.

  4. Victor Lava Avatar
    Victor Lava

    Worked like a charm. Thanks for a code share! 🙂

  5. Camp Trend Avatar
    Camp Trend

    Thanks so much, works perfect!!

  6. Nghia Avatar
    Nghia

    I pasted the code into my function.php but when i view my RSS source, i dont see any images to my items. I’m using the WP featured image function of WP (running latest version); What am i doing wrong?

  7. 8 Things Every WordPress Developer Should Know | Tips

    […] Source: WordPress: Add featured image to RSS feed/ […]

  8. Pratish Avatar
    Pratish

    Thanks for this Hugh – needed it for a cartoon blog I am running and relieved at the thought of not having to add in another plugin! I’m curious to know if there is any way of overlaying some message on the thumbnail in the feed (e.g. click on cartoon to view full image on the site) – or even if I could insert the message under the featured image in the feed itself it would be great… any suggestions please?

    Thanks again!

    1. Hugh Lashbrooke Avatar
      Hugh Lashbrooke

      Hi Pratish – the $output variable merely contains the HTML of the tag, so all you would need to do is add more text/HTML onto the beginning or end of that variable and it will show up as you have defined it. You have the global $post variable available so you can use that to display any dynamic post data you want at this point.

      1. Pratish Avatar
        Pratish

        Thanks for the reply Hugh – sorry, I’m bit of a newbie so need a bit more help on this please. I’ve also been trying to get the thumbnail in the RSS feed to link to the actual post (so RSS readers can click on the image to see the full cartoon on the website). I’m honestly not sure how to tailor these two lines to include the “a href” to achieve this:


        $output = get_the_post_thumbnail( $post->ID, 'medium', array( 'style' => 'float:right; margin:0 10px 10px 0;' ) );
        $content = $output . $content;

        1. Hugh Lashbrooke Avatar
          Hugh Lashbrooke

          Hi Pratish,

          You could modify that code to look something like the code at this link: http://pastebin.com/raw.php?i=LGQFYXks

          I haven’t tested that code specifically, but it should wrap the image in a link to the post.

          1. Pratish Avatar
            Pratish

            Thanks for the Hugh – I tried it out and it crashed my feed… managed to fix it though :). The working code, including the link to the post from the thumbnail should read:


            $output = get_the_post_thumbnail( $post->ID, 'medium', array( 'style' => 'float:left; margin:0 10px 10px 0;' ) );
            $output = 'ID ) . '">' . $output . '';
            $content = $output . $content;

          2. Pratish Avatar
            Pratish

            Oops sorry I thought using the code tags would allow me to write the href too – I can send you the code if you’d like to update it. Basically the rel=”nofollow” bit in your pastebin thing was in the wrong place so it didn’t work.

          3. Hugh Lashbrooke Avatar
            Hugh Lashbrooke

            Not sure how that rel=”nofollow” bit got in there – wasn’t supposed to be. Glad you got things working!

          4. Jose Avatar
            Jose

            Pratish, Hugh,

            Thanks for sharing the solution… was thinking of how to link the image itself. Though not sure if I got it right: http://d.pr/n/2idb

            Appreciate your look at it…

  9. 8 Wordpress Tricks A Developer Must Know | Create Website in Minutes

    […] Source: WordPress: Add featured image to RSS feed/ […]

  10. Lanaya | Raising Reagan Avatar
    Lanaya | Raising Reagan

    Hi Hugh …

    I’ve added the code to my php and it still doesn’t work. I have added all kinds of suggestions to the code, yours had a slight variation that I was hoping would fix the problem. I am SO baffled because I can’t think of anything else to do.
    I don’t know if it’s something in my overall Feedburner that isn’t picking it up?

    I would really love your expertise. I can send you anything you need to look at it.

    Thank you so much.
    Lanaya
    http://raising-reagan.com

    1. Reid Avatar
      Reid

      Is there a logical progression from this snippet that would put the featured image in its own tag in the RSS feed (outside of the content). It would be nice to lead with the featured image, then display the title, date, then content.

      Any thoughts?

      I understand it would require editing the feed-rss2.php file in some way right?

      1. Hugh Lashbrooke Avatar
        Hugh Lashbrooke

        To get the image above the content all you’ll need to do is remove the CSS from this snippet, so just remove this code: “, array( ‘style’ => ‘float:right; margin:0 0 10px 10px;’ )”. If you’re using the image as a header image then you may also want to change the size from ‘medium’ to ‘large’ (or any other custom size defined by your theme/plugins). That won’t get it above the title and date though – you’re right in thinking that you will have to modify the RSS template for that.

    2. Hugh Lashbrooke Avatar
      Hugh Lashbrooke

      If you’re using Feedburner then the issue most likely that Feedburner is caching your feed and it will only refresh the new content at some point in the future. Other than that there’s no reason why this snippet wouldn’t work.

  11. George Avatar
    George

    Thanks for sharing this! I have an odd related question: would it be possible to add code that includes the URL to the featured image, but doesn’t display it on the visible feed item?

    I am looking to pull the Featured Image URL from each of my posts via the RSS feed, and display that image — hyperlinked to the related post — on a separate website. Basically I want to display thumbnails for current posts from the blog on the sidebar of my art portfolio (the art portfolio site is not a WP site, it’s a hand-coded site).

    Since my posts already have the same image used for the Featured Image inserted into the body of the post, adding your code results in posts with the image included twice. Is there a way to include the Featured Image URL “behind the scenes”, so it is included in the RSS XML code, but not displayed to the viewer?

  12. Nimrod Avatar
    Nimrod

    This is a very handy snippet! ..But I’m using a purchased theme so I prefer to use it as a plugin to avoid having to keep track of which files I have to check after an update of the theme. I also used to hard code changes directly into my theme files but I find it very messy when I have to update to the latest versions..

    1. Hugh Lashbrooke Avatar
      Hugh Lashbrooke

      You should always be using child themes to prevent issues when updating your theme. More info on that here: http://codex.wordpress.org/Child_Themes

  13. Kenin Bassart Avatar
    Kenin Bassart

    Hi, I had a quick question for you. I am trying to implement this solution to include the featured image in the post when there is only a snippet of the feed being used. When I view the full post in the feed my images render fine, but I would like the snippet feed to show a picture first, then the first few lines of text. Is this possible?

    1. Hugh Lashbrooke Avatar
      Hugh Lashbrooke

      You should be able to achieve this by replacing ‘the_content’ with ‘the_excerpt’ in the add_filter() call, but I haven’t tested that to be sure.

  14. mywebprogrammer.com Avatar
    mywebprogrammer.com

    Good plugin but doesn’t enable you to style the featured image in your RSS feed. Can’t change the size of the featured image.

    1. Hugh Lashbrooke Avatar
      Hugh Lashbrooke

      Well this isn’t really a plugin – it’s just a code snippet that you should edit yourself to suit your needs. You should never just copy and paste other people’s code snippets without seeing if you can customise it for your use case. Besides, you’re wrong about not being able to style the image – you can use any CSS you like in the ‘style’ parameter inside the get_the_post_thumbnail() function to do this.

      1. Trude Avatar
        Trude

        Hi, and thanks for a great explanation about rss feeds and images. You say one should add the code in the theme’s functions.php file. I have never done this kind of code editing before but I have read that one should do changes in the child theme. Is this correct?

  15. Melody Avatar
    Melody

    I’ve input the code into my Child Theme Functions.php and I still can’t get the images to pull in my RSS feed. Any other suggestions?

  16. Gerald@wpcustoms Avatar
    Gerald@wpcustoms

    Have you tried using the_content_feed instead of the_content as the filter?

    1. Hugh Lashbrooke Avatar
      Hugh Lashbrooke

      That would actually be a great improvement to this – I wrote this snippet some time ago and I think it definitely needs an upgrade. I’ll implement that sometime soon 🙂

  17. ayan Avatar
    ayan

    Hello all here, I have a wordpress blog and I tried to use this code but it is not working correctly, Can you guide me what version of wordpress is needed for this to work.

  18. Mohammed Gritli Avatar
    Mohammed Gritli

    dear,
    thank you for this function , but, am building an rss iOS app, how can i get the image link so i can add it to my app

  19. ashley Avatar
    ashley

    thank you. just added. very helpful!

  20. Michael Mooney Avatar
    Michael Mooney

    I just used your code as I am using our sites xml feed in our new business app I’m building and it works very nicely.

    It’s a very nice, clean bit of coding.

    Thanks for sharing ;D

  21. Rick Nelson Avatar
    Rick Nelson

    Just used the code
    Been looking for something like this
    Thank you so much Hugh

  22. Nivi Morales Avatar
    Nivi Morales

    Hello!
    I extended your snippet a little bit to add a default image if the user has not set up one.

    function featured_image_in_feed( $content ) {
    $default_thumb = get_bloginfo(‘stylesheet_directory’).’/images/warks-news.png’;
    global $post;
    if( is_feed() ) {
    if ( has_post_thumbnail( $post->ID ) ){
    $output = get_the_post_thumbnail( $post->ID, ‘medium’, array( ‘style’ => ‘width:290px; height:auto; float:right; margin:0 0 10px 10px;’ ) );
    $content = $output . $content;
    } else {
    $output = ”;
    $content = $output . $content;
    }

    }
    return $content;
    }

    add_filter( ‘the_content’, ‘featured_image_in_feed’ );

    Super useful. Thank you! 🙂

  23. Dave Iannone Avatar
    Dave Iannone

    Anyone have a snippet that would put the Featured Image as a SEPARATE component of the RSS feed, not integrated into the content block? For example URL of Featured Image

  24. Jenn Avatar
    Jenn

    Got this working very easily.

    I was originally going to use a plugin, but it was incompatible with the Feedburner plugin. Adding the snippet above to the functions.php file works beautifully. Thank you!

  25. Martin Avatar
    Martin

    Thanks, this has been bugging me for ages!

  26. Techniblogic Avatar
    Techniblogic
  27. Dave Avatar
    Dave

    Thanks for posting this. I tried pasting the code into my functions.php file in the theme root directory, however it just crashes my website and I have to re-upload the original file in order to get the website back up again. Any ideas?

    1. Alex Avatar
      Alex

      Dave, you must pasta only this:
      add_filter( ‘the_content’, ‘featured_image_in_feed’ );
      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;
      }

      without

  28. Damian Avatar
    Damian

    Hello Hugh,

    I have 2 questions… I also have a MyBB forum and it pulls the feed. I’m using your code so an image will be included. How can I center the image above the excerpt?

    And, how can I add social links under the excerpts, Example Facebook etc… I see this done with some feeds and can’t figure how to get it done.

    Regards

  29. Schalk Avatar
    Schalk

    Hi Hugh, trust your well!

    Anyway to modify your feed function to include the price of woocommerce products to the product feed?

    Dankie
    Schalk

  30. Schalk Avatar
    Schalk

    Hi Hugh, manage the price part, somehow.
    Problem is the price is part of content, so I can’t style it.
    https://www.dropbox.com/s/pf5znskv4to1szu/woocommerce-product-feed.png?dl=0

    Lekker dag,

  31. Paknode Avatar
    Paknode

    Your snippet is working great, an I am sure you will update it according to new versions of wordpress. We all need such creative persons like you who can give us some thing to cheer about. Also wordpress blog platform are the best on this planet.

  32. techtutorialsbd Avatar
    techtutorialsbd

    Thank you for sharing this article -Add featured image to RSS feed .

  33. Bolkya Resha Avatar
    Bolkya Resha

    I tried same for my site http://www.bolkyaresha.net but rss feed not showing images,plz help

  34. Hasna Avatar
    Hasna

    Hi Hugh,

    I’m a noob in wp or coding. I just came here when searching for featured image broken in IFTTT recipe. I’m very interested to apply this code in my functions… And it makes me noticed, that (maybe) something wrong with my feed at all. It just shown the Unicode in the browser when I typed /feed.

    Could you help me? Thank so much.
    sepatumoo.com/feed
    This is the problem I faced and googled for a while.

Leave a Reply

Your email address will not be published. Required fields are marked *