Adding a custom image node to a WordPress RSS feed

I’ve been working with a web development firm to update the homepage of my employer’s website. One of the features of the updated homepage is a nicely styled newsfeed that pulls in both news content and the latest posts from our externally hosted WordPress blog. The developers asked me to add a custom node to the RSS feed of the WordPress blog (which I manage) with the featured image URL in it so they could embed that image in a news feed on the homepage. In other words, they wanted me to tweak the RSS feed so it included this bit of markup in each item:

<image>featured_image_url</image>

After trying about a dozen plugins off the shelf and poring over many hanging support threads that never resolved the issue, I realized that there isn’t an up-to-date WordPress plugin out there to do this. After consulting the WordPress codex Function Reference and hacking two plugins that get sort of close (SB RSS Feed Plus and Featured Image in RSS), I figured it out. Here I share with you the successful results.

UPDATE: Sage Lichtenwalner has suggested two much better ways to accomplish the same results. The fastest and easiest one is to go to the third link in his comment and copy and paste the image node example code straight into your functions.php file (in your child theme folder, of course).

1. Define a new function that outputs the URL of the post’s featured image.

One way to do this is to append just the function code below (everything from the word “function” to ) to the end of your theme’s functions.php file (which is editable from the theme editor in the WordPress dashboard). A beter way to do this, if you’re using a child theme, is to define the function in a new functions.php file in your child theme main folder. WordPress will append it to the functions defined in the parent theme and in the core of WordPress. Here’s what my functions.php file in my child folder looks like:

< ?php
/**
*Outputs the featured image URL for use in RSS2 feed
*
*/
    function feed_getFeaturedImage() {
        global $post; if( function_exists ('has_post_thumbnail') && has_post_thumbnail($post->ID)) {
            $thumbnail_id = get_post_thumbnail_id( $post->ID );
            $thumbnail_url = wp_get_attachment_url($thumbnail_id);
        }
        return ($thumbnail_url);
}

2. Edit your WordPress RSS template (/wp-includes/feed_rss2.php) to include the new node and call the new function you’ve created.

In my case I’m simply going to the paste the following code in feed_rss2.php wherever I want the new node to appear:

<image><?php echo feed_getFeaturedImage(); ?></image>

As it stands, this solution requires the user to customize code (the RSS template) that may get overwritten every time WordPress updates. I would like to figure out a way to do this via plugin or without editing anything but child theme elements. But for now, this does the job.

I don’t know much about PHP, other than that it doesn’t appear to be all that different from the other programming languages I’ve learned. I’m also not a WordPress guru. So I leave it to the WordPress and PHP experts out there to amend my solution with more best practices.

16 thoughts on “Adding a custom image node to a WordPress RSS feed”

  1. Is this thread still active?
    Been scouring the internet hoping to find a solution to my problem.

    Issue is, my RSS Feed is displaying media files (images), with a dimension extension, meaning the rss feeds are pulling the smallest of the media files to display in my feeds.

    My intention is to pull the largest of the images available to use with pinterest.
    I cannot do that with the current setup of my feed as I am getting images as small as 150×150 when, in the same folder, there are images 500 x 500px.

    I have installed a plugin called “Featured Images in RSS w/ Size and Position” but this plugin’s adding the images into the content of the RSS Feeds, and not replacing the images in the “enclosures” tags, which seem to be the location where the images are being pulled from when using my feeds with Pinterest.

    URL of feed in question:
    http://www.ishopvillage.com/Christmas-Gift-Ideas/feed/

    Any work around to this would be highly appreciated.. I’m about to pull my hair regarding this.

    Thanks in advance.

  2. Adjusted version for link/image.jpg
    add_action(‘rss2_item’, ‘add_my_rss_node’);

    function add_my_rss_node() {
    global $post;
    if(has_post_thumbnail($post->ID)):
    $large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), ‘large’);
    echo(“{$large_image_url[0]}”);
    endif;
    }

    1. When copying and pasting this code snippet (and the other snippets on the page), make sure to replace the curly ” and ‘ signs with regular non-curly ones, or you’ll cause a PHP syntax error.

  3. I tried Sage’s 3rd suggestion, and it works like a charm to a large extent. My problem is that the reference in the node is to the attachment page.

    Is there some way to get the URL of the image in question?

    1. Hi Karl, try using wp_get_attachment_url() instead of get_attachment_link(). Not sure why the example uses the latter function.

      Here’s the tweaked code…

      add_action(‘rss2_item’, ‘add_my_rss_node’);
      function add_my_rss_node() {
      global $post;
      if(has_post_thumbnail($post->ID)):
      $thumbnail = wp_get_attachment_url(get_post_thumbnail_id($post->ID));
      echo(“{$thumbnail}”);
      endif;
      }

  4. Hi, Sandra!
    Google sent me to your blog, while looking for a solution to the same problem. However, I can’t see the comment Sage Lichtenwalner posted, about solving this easier.

    Could you paste his comment again, please?
    Thanks,

    Silviu, a blog reader from far away 🙂

    1. Sure. Here’s what Sage wrote:

      “I never realized feeds weren’t so easily theme-able. Luckily there are less drastic measures available than editing core files. (Trust me, you don’t want the coding gods to banish you to their syntax-error filled purgatory.)

      “You could copy the default feed template file to your (child) theme, and use the following function to force WP to use your version.
      http://codex.wordpress.org/Customizing_Feeds

      “This appears to be exactly what this plugin does.
      http://wordpress.org/plugins/feed-template-customize/

      “However, I think all you really need to do is use the rss2_item hook to inject your thumbnail function into the feed.
      http://codex.wordpress.org/Plugin_API/Action_Reference/rss2_item

      “You could also consider creating a custom feed using the approach above just for the main company homepage, say if you wanted to display only the excerpts with featured images there, while allowing others to subscribe to the blog’s full feed.”

  5. I implemented your suggestion Sage, but it seems to only put the URL in the tag. To display in feeds, don’t you need the url, link and title?

    1. I’m not an expert in RSS, but it looks like you might be right.
      http://www.w3schools.com/rss/rss_tag_image.asp

      So I’m not exactly sure how this technically incorrect syntax works for Sandra, nor why only the url is included in the example, but my guess is that some browsers and feed readers support the bad syntax. (I haven’t really tried this, so I don’t really know.)

      What amazes me more is that most solutions out there seem to use either a tag which is completely unsupported, or simply add a thumbnail to the regular content as a traditional html img tag. I’d suggest using the latter approach, if you don’t strictly need strict RSS. Otherwise, you can easily modify the code you’re using to add the url, link and title fields.

  6. I never realized feeds weren’t so easily theme-able. Luckily there are less drastic measures available than editing core files. (Trust me, you don’t want the coding gods to banish you to their syntax-error filled purgatory.)

    You could copy the default feed template file to your (child) theme, and use the following function to force WP to use your version.
    http://codex.wordpress.org/Customizing_Feeds

    This appears to be exactly what this plugin does.
    http://wordpress.org/plugins/feed-template-customize/

    However, I think all you really need to do is use the rss2_item hook to inject your thumbnail function into the feed.
    http://codex.wordpress.org/Plugin_API/Action_Reference/rss2_item

    You could also consider creating a custom feed using the approach above just for the main company homepage, say if you wanted to display only the excerpts with featured images there, while allowing others to subscribe to the blog’s full feed.

    1. Yay, thanks Sage! I tried the hook method and it’s super easy – just pasted that code at the link directly into the functions.php file for the child theme. I read some super self-assured introduction to the hook somewhere, but the article gave me no clue how to properly implement it.

      So far I’ve stuck to simply theming, but this makes me thinking I need to learn more about constructing plugins.

      1. Glad I could help.

        You could turn this snippet into a plugin, and it might be a good exercise to figure out how to set one up. But in general, if you’re already using a child theme and you’re okay with just adding tweaks where you need them, there’s no need to dive headlong into plugin development. I’d save that fun for a more ambitious endeavor.

Leave a Reply