Backup of David's Livejournal

Adding the image tag to Wordpress's rss headers


The RSS specification allows you to have an <image> element in the header of your RSS feed.  A long time ago, if you used Wordpress, you'd have to edit the wp-rss.php file and be very careful when you upgraded to new releases, or your change could be lost.

But if you use a modern version of Wordpress, you should take advantage of the do_action('commentsrss2_head') hooks* in the feed-rss... .php files.

Here's what you'd do: Add the following code to your theme's functions.php file:

function add_my_rss_image()
{
echo '<image><title>', bloginfo_rss('name'), '</title>';
echo '<url>', bloginfo_rss('stylesheet_directory'), '/images/button.gif</url>';
echo '<link>', bloginfo_rss('url'), '</link>';
echo '<width>88</width><height>31</height>';
echo '<description>Description of your blog.</description></image>';
}

add_action('rss2_head','add_my_rss_image');
add_action('rss_head','add_my_rss_image');
add_action('commentsrss2_head','add_my_rss_image');

Take care to actually put an image file in the path specified, and adjust the width and height accordingly.

Et, voila! Now your feeds have images, and they're forwards compatible with future versions of Wordpress. Oh, did I say future versions of Wordpress? Excuse me, I have to:

svn sw http://svn.automattic.com/wordpress/tags/2.6/

Blam! Upgraded. Sweet.

* Also rss2_head and rss_head, just so I cover the appropriate Google terms.

Comments

 wiuomybhvlxa on Jul 28th 2008 at 9:42 PM
(Deleted)

 kaixin520 on Jul 28th 2008 at 9:42 PM
(Deleted)

 bannsunivirs on Jul 28th 2008 at 9:42 PM
(Deleted)

 heroes10 on Jul 28th 2008 at 9:42 PM
(Deleted)

 rqqbnpgjmxjr on Jul 28th 2008 at 9:42 PM
(Deleted)

 pglxchwaoswt on Jul 28th 2008 at 9:42 PM
(Deleted)

 (no name) on Aug 10th 2008 at 4:11 PM
Hi bloginfo_rss('stylesheet_directory') would probably be better than bloginfo_rss('url') so that you could use an images folder in the theme itself. Very helpful function, many thanks.

 dblume on Aug 10th 2008 at 5:05 PM
Excellent suggestion, thanks!

 (no name) on Sep 23rd 2008 at 12:04 AM
Perfect news!, man

 (no name) on Sep 28th 2008 at 12:03 PM
nice work, brother

 festootfj on Feb 21st 2009 at 6:12 PM
What a good feature, many thanks. I am using Wordpress 2.6 and my rss feed have an image i specified to display. -Cars

 (no name) on Apr 16th 2009 at 10:15 PM
I added this code to my functions.php but didn't get the desired result. you said to: "Take care to actually put an image file in the path specified, and adjust the width and height accordingly." I'm not sure I know what you are saying here. There are images in every one of my articles. Am I supposed to do more than paste the code into the functions.php theme file? I'd appreciate help for anyone. Thank you, Sharon smkyle1@yahoo.com

 dblume on Apr 16th 2009 at 10:32 PM
This tip doesn't have anything to do with images in your blog's articles. It only describes a WordPress-compatible way to provide an image to associate with the entire feed itself. (This is a rarely-used feature.) Refer to the link at the top of this post, and examine only the section, "<image> sub-element of <channel>", to see what this is about. If you get it, and can read XML, and understand the intent of the RSS schema, then this tip is for you. To verify you've done it correctly, you may want to cURL your feed's address and visually inspect the <channel> element. Most people don't concern themselves with this sort of thing.

 (no name) on May 14th 2009 at 7:54 AM
I use WP 2.7.1, and recently inserted the code into my functions.php, but no image show up in rss reader. No wonder: the lines defined in the function are simply not echoed in the source of my rss file, as if function did not executed at all at rss_head. The same applies to 'blog icons' plugin, which I tried previously and works fine, except that image related lines in rss source are also absent. Curiously, plain favicon related code is echoed perfectly in index.php header. I wonder, if rss_header hooks work in recent WP version just the same as used to?

 dblume on May 14th 2009 at 2:16 PM
Yes, it works in WP 2.7.1, and even through redirects through feedburner! See my family blog for an example. View source to convince yourself of my WP version. (The feed actually redirects to http://feeds2.feedburner.com/dlma/my, which retained the image tag.)

 (no name) on May 14th 2009 at 9:13 PM
Well, I'm perplexed. After my first comment, I removed the code from functions.php (didn't want to left behind code which didn't seem to work), but I left 'blog icons' plugin (which is basically just the same, with an admin interface) installed. I know it sounds silly, but since then, the image related code "appeared" in all 3 feeds over time, first in the Atom and rss feeds, then at last in the rss2 feed too. Now it's all fine. Maybe it was some kind of a caching issue, or something like that. (By the way, do you know how to use feed favicons with rss? Atom can specify it, but rss doesn't. Somehow, most rss feeds I'm subscribed to show their favicons in the feed reader. Maybe the reader automatically pick up the site's regular favicon?) greetz! Charlie Brown

 hjzpqduvobgw on May 18th 2009 at 11:37 AM
I really like how you did this but unfortunately I cant get it to work on my site. I think my code is a bit different and Im not sure which bits are affecting the outcome. Ill keep trying because I really like the result.nancy

 (no name) on Jul 15th 2009 at 1:31 PM
I did not know this! It's a good idea, Thanks for the code, I'll try it on my blog! voyance

 nuochoa on Jul 28th 2008 at 9:42 PM
(Deleted)

 (no name) on Sep 14th 2009 at 2:21 PM
I don't want to sound like an idiot but this is all very new to me. All I want is to add an image to my header. This is my page: http://lchoafwb.org/naranonfl/ I would love to have a background image with our logo but I definitely want one to the left of the wording Nar-Anon Florida Region. Just to the left of that would be perfect. Can someone please help me? Thanks.

 dblume on Sep 14th 2009 at 3:48 PM
The subject of this post has nothing to do with any of the images displayed at your website. It's only about putting an image in your blog's RSS feed's header, which is a detail that most people won't worry about. When it comes to customizing your blog's "header" the way you want, that's customizing your blog's theme's header. You'll have to learn about customizing themes. You may find help with that over at Wordpress.org. Good luck!

 nuochoa on Jul 28th 2008 at 9:42 PM
(Deleted)

 (no name) on Dec 15th 2009 at 1:38 PM
Hello David, Thank you very much for the article! Do you happen to know how does one add the alternate text for the image included in the RSS feed? Cheers, Nik

 (no name) on Feb 13th 2010 at 2:48 PM
Just a hint for some: an easier way to get an image in your feed's header is to burn your feed with feedburner (use the help provided there), and use THAT service to do all sorts of things with your feed - a.o. display an image. No need to get into functions.php.

 (no name) on Feb 22nd 2011 at 6:46 AM
(Spam Removed)