Display custom field images in RSS Feeds

Result in Google Reader

If you’re reading this, I guess you know how frustrating it is when custom field images don’t display in your RSS Feed. This is because the feed only outputs your actual “content” by default (as in what you write in the editor), but not custom fields.

For example, if you take a look at this theme page on this site, you’ll notice the “features” box – that is done through custom fields. Now if you check it out in your RSS reader, that box will not appear, simply because RSS feeds don’t display custom fields by default.

The workaround

Lucky us, Justin Tadlock has created a simple but effective WordPress plugin that solves the problem.

Download the plug-in here.

Installation and set-up

  1. Extract the plugin to a folder and open the custom-fields-for-feeds.php file in your editor of choice.
  2. Delete all and and paste the following code:
add_filter('the_content', 'custom_fields_for_feeds');
 
function custom_fields_for_feeds( $content ) {
 
global $post, $id;
$blog_key = substr( md5( get_bloginfo('url') ), 0, 16 );
if ( ! is_feed() ) return $content;
 
// Place your own four custom fields by replacing the capital text
	$image = get_post_meta($post->ID, 'post_image_url', $single = true);
	$image_alt = get_post_meta($post->ID, 'post_image_alt', $single = true);
	$image_width = get_post_meta($post->ID, 'post_image_width', $single = true);
	$image_height = get_post_meta($post->ID, 'post_image_height', $single = true);
 
// If there's an image, display the image with the content
// Feel free to change the styling of the image border
	if($image !== '') {
	$content = '
 
	<img src="'.$image.'" alt="'.$image_alt.'" width="'.$image_width.'" height="'.$image_height.'" />
 
' . $content;
	return $content;
	} // End if image
 
// If there's not an image, just display the content
	else {
	$content = $content;
	return $content;
	}
} // End function

Save, then upload and activate the plugin.

Now, when creating a post and assigning an image through custom fields, just use the values you specified earlier:

  • post_image_url = the absolute URL to the image (ex. http://www.mysite.com/wp-content/uploads/…..)
  • post_image_alt = the ALT text for the image (for accessibility and a little SEO)
  • post_image_width = the width of the image in pixels (ex. 600)
  • post_image_height = the height of the image in pixels(ex. 400)

Why I advised in step 2 to delete everything? Well, Justin’s awesome plug-in handles much more than just images, but it can get a little confusing if you only need images to show up. Consider it a “simplified” version.

Thanks Justin for the plugin!

    Please share it if you liked it:



Related Posts

  1. Display ads in WordPress depending on what site the visitor comes from
  2. WordPress Ecommerce Plugin – PHPurchase
  3. WordPress Membership Plugins and Resources
  4. WordPress Ecommerce Plugins, Themes and Tutorials
  5. How to remove title tags in WordPress

6 Comments For This Post

  1. Norbert

    So it is possible to theoreticly any custom field value to a rss-feed. But have you any idea how to read out a single custom field value from a rss-feed instead of (only) recieving it with the content?
    Thanks you very much for sharing

  2. ThemeDigital

    Hey Norbert,

    Thanks, glad you found it helpful :)

    Not sure I understand your question, can you please elaborate on “read out a single custom field”?

    Cheers!

  3. kim

    I tried this and it doesn’t show up in WP after I upload it. Are we supposed to delete everything? At the end, is there supposed to be the line of code…

    ?>

  4. ThemeDigital

    Hey Kim,

    Sorry, WP took out your code in the comment.

    I’m using a plugin to highlight the code on my blog, and sometimes it messes everything up.

    I’ve updated the post, it should work for you now :)

  5. Rich Gubby

    Nice post – I’ve written a plugin too that you might find useful.

    It’s called RSS Custom Fields and it displays all custom fields in your feed, check it out: http://wordpress.org/extend/plugins/rss-custom-fields/

  6. ThemeDigital

    Thanks for letting us know Rich, it’s definitely a useful plugin :)

What do you think of this?