Add Custom Values To Your WordPress RSS Feed

As most of you probably know, Script & Style was created by myself, David Walsh, and CSS-Tricks scribe Chris Coyier.  We were both looking to create widgets for our website that would provide not only a link to the article submission page on Script & Style but also a direct link to the full article on the author’s domain.  Since we were reading in the RSS feed to grab articles and links, we needed to add a custom XML key/value to the RSS feed so that we could retrieve the direct link URL.  Here’s how we did it.

Hacking WordPress’ feed-atom.php File

WordPress creates two different feeds:  a basic RSS feed and an Atom feed.  Script & Style, like almost every other blog out there, uses FeedBurner to serve the syndication feed, and we chose to have FeedBurner use the Atom feed.  In our experience, the Atom feed has been more stable and doesn’t cause problems in Google Reader like the basic RSS version does from time to time.  Since we’re serving the Atom feed, we’ll need to modify the feed-atom.php file.

The code to complete this mission is short and sweet:

<entry>
		<author>
			<name><?php the_author() ?></name>
			<?php $author_url = get_the_author_url(); if ( !empty($author_url) ) : ?>
			<uri><?php the_author_url()?></uri>
			<?php endif; ?>
		</author>

		<!-- this is what we add -->
		<directurl><?php echo get_post_meta($post->ID,'Author Webpage',1); ?></directurl>

		<title type="<?php html_type_rss(); ?>"><![CDATA[<?php the_title_rss() ?>]]></title>
		<link rel="alternate" type="text/html" href="<?php the_permalink_rss() ?>" />
		<!-- ...more fields below... -->

The direct URL is held in the database as a custom field called “direct_url.”  Since there’s a relationship in the database between the article and the direct URL, all we need to do is use WordPress’ get_post_meta() function to retrieve the field we want.

The Result

Our feed at FeedBurner now looks like:

You can place the new XML key/value anywhere within the “<entry>” element.

That’s all you need to do to add custom key/values to your RSS feed.  Now that you know how to do it, what would you use this for?

Comments

[...] over to Script & Style to check out the full [...]

This hack will work - but you will lose you changes when you update wordpress. Anyway to solve this problem?

Binny V A | September 22nd, 2008 @ 11:52 am

Unfortunately, an upgrade of the file will overwrite the custom parameter. Fortunately, the code is so short that placing it back in wont be a problem.

admin | September 22nd, 2008 @ 12:05 pm

[...] Visit Source. [...]

Add Custom Values To Your WordPress RSS Feed | September 22nd, 2008 @ 4:04 pm

[...] Fortunately, the direct URL’s are stored in the database along with each post, so David hacked up the RSS feed to include them, so we could parse them out and use them on our own [...]

Links of Interest - CSS-Tricks | September 23rd, 2008 @ 5:46 am

If you drop me a quick email, I can send you a plugin I’ve quickly written up that takes care of this. Better yet, I’ll write a quick post.

Joost de Valk | September 23rd, 2008 @ 11:46 pm

@Joost: Good idea!

admin | September 24th, 2008 @ 5:45 am

[...] Add Custom Values to Your WordPress RSS Feed - Script & Style [...]

[...] Add Custom Values to Your WordPress RSS Feed - Script & Style [...]

[...] If you have a self-hosted Wordpress blog, have you ever wondered how to add custom values to your RSS Feed? David Walsh explains it all at Script & Style! [...]

[...] If you have a self-hosted Wordpress blog, have you ever wondered how to add custom values to your RSS Feed? David Walsh explains it all at Script & Style! [...]

[...] If you have a self-hosted Wordpress blog, have you ever wondered how to add custom values to your RSS Feed? David Walsh explains it all at Script & Style! [...]

[...] If you have a self-hosted Wordpress blog, have you ever wondered how to add custom values to your RSS Feed? David Walsh explains it all at Script & Style! [...]

KM4pQv hi nice site thanx http://peace.com

bob | January 3rd, 2009 @ 5:18 am

I am not a programmer. I downloaded wp 2.7 and cannot get feedburner to work because apparently the feed doesn’t work on my site. I’ve tried everything over the last two days. what file do I need to edit to give the site a correct feed and what should the code be? I don’t care if it is rss, rss2 or atom at this point, I just want it to work. thanks.

dottie | January 14th, 2009 @ 2:28 pm

[url=http://www.hookup.com][img]http://verifiedfile.com/images/sex.gif[/img][/url]

Greetings Everybody !

These days there are a lot of live video dating sites. Today I found myself on [b]Hookup.com[/b]. It’s rare that I spend 2 hours on a site, so here I am hyping like an 18 year old.

[url=http://hookup.com][b]Hook Up[/b][/url] really took me off guard because of it’s [url=http://www.hookup.com][b]Free Adult Webcam[/b][/url] services. Basically, girls get naked on camera and you can chat with them.

I’m not much of a hyper - so don’t get me wrong - but this video I posted is probably one of the funnier videos I’ve seen in a while from a dating site. Pretty outrageous.

Anyway, my new motto is Bare it & We’ll Share it!

[url=http://hookup.com][b]Adult Search Engine[/b][/url] vs [url=http://www.hookup.com][b]Adult Webcam[/b][/url]

arracrork | March 26th, 2009 @ 1:08 am