Adding Feature Image to WordPress RSS
So, I wanted to pull in the feature image to a WordPress RSS feed. It took me a little while to figure it out but it was easy enough in the end. The finished product looks like this:
add_filter('the_category_rss', 'add_feature_image');
function add_feature_image( $content ) {
global $post;
if(has_post_thumbnail( $post->ID )){
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' );
return $content . '<featureimage>' . $image[0] . '</featureimage>';
}
else return $content;
}
So, I first add a filter to the category rss output. Why category I hear you ask, because when you look at /wp-includes/feed-rss2.php you’ll see that the category output in the rss is the only one that outputs it’s own tags. For example, the link tag looks like this:
<link>< ?php the_permalink_rss() ?></link>
So it’s no good for adding tags to. the_category_rss() outputs returns the category names in a tag called category. With me?
Then the filter runs a function (add_feature_image()) which checks to see if there is a feature image and if it does, appends a featureImage tag with the image’s url to the end of the categories.
The above is simply packaged up into a plugin and enabled in the admin section.
Easy!