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.