How to Create a Custom RSS Feed in WordPress in 12 Lines of Code

April 2, 2009

in Tutorial, WordPress

Custom RSS feed me!

Custom RSS feed me, Seymour

Recently I wanted to create a feed for Google base that used a bunch of custom fields that I had created. It wasn’t easy to find what I was looking for online, so I thought I’d show how simple it is to make a custom feed & feed template in WordPress.

All it takes is one piece of code to point to your own feed template and create an unique feed name.

function create_my_customfeed() {
load_template( TEMPLATEPATH . 'your-custom-feed.php'); // You'll create a your-custom-feed.php file in your theme's directory
}
add_action('do_feed_customfeed', 'create_my_customfeed', 10, 1); // Make sure to have 'do_feed_customfeed'

Now, rewrite your feed like WordPress does, all pretty-like.

This has set up everything you will need to modify your feed. You will now be able to access your feed at http://yoursite.com/?feed=customfeed. But what about how WordPress does the really neat rewriting of feeds from /?feed=rss to /feed/rss/? That requires using generate_rewrite_rules(), as described in the WP_Rewrite Codex page.

This piece of code will allow you to have any feed name, and dynamically “create” /customfeed.xml as well as a /feed/customfeed/ versions:

function custom_feed_rewrite($wp_rewrite) {
$feed_rules = array(
'feed/(.+)' => 'index.php?feed=' . $wp_rewrite->preg_index(1),
'(.+).xml' => 'index.php?feed='. $wp_rewrite->preg_index(1)
);
$wp_rewrite->rules = $feed_rules + $wp_rewrite->rules;
}
add_filter('generate_rewrite_rules', 'custom_feed_rewrite');

You’ve set up your custom feed locations, now you need a feed template!

In the first piece of code, we told the create_my_customfeed() function to look in your theme’s directory for a your-custom-feed.php file. This will be the feed template your feed will use.

For this part, go to /wp-includes/feed-rss2.php and copy and paste the code wholesale into your custom feed template.  From there, you can start modifying away at the code.  All the WordPress functions you’re used to are still available, because you are still in the WordPress loop.  

Now, check out your feed and see if it works…it should!

One way I have applied this code

I used this code when I created a Google Base Feed for CellPhoneFiesta.com. It took custom fields from each post and created a compliant feed.  Check out the Google Base feed that uses this code.

Any questions?

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • StumbleUpon
  • Technorati
  • Twitter

Related posts:

  1. Add a Store Catalog XML Sitemap to the Shopp Plugin

{ 2 trackbacks }

Weave Snatch » Blog Archive » haiku update!
May 3, 2009 at 4:37 pm
Liste d’annuaires de flux RSS
September 12, 2009 at 9:14 pm

{ 42 comments… read them below or add one }

kwbridge April 2, 2009 at 11:17 am

Hi Zack – This looks like something I’ve been thinking about doing. The thing is I have too many questions. Is this for the site feed or for a variety of feeds that you want to add on a page? Do you have feeds listed in the custom fields that are being pulled to the custom field page?

Would it be possible to write a step by step tutorial (even for the parts you think are obvious) for noobs like me? ;-)

Thanks!

Reply

kwbridge April 2, 2009 at 11:59 am

I think so :-)

I’ll take a look at those plugins.

But really what we are doing is pulling 4 or so RSS feeds into one WP page – not into one feed – like 4 feed blocks. I found a plugin to do that but I thought if I used custom fields for the feed urls it might make it easier to use. A lot of this is still in the working it out in my head phase.

Thanks!

Reply

anthonylrivera April 9, 2009 at 10:38 am

You are the MAN !
I was plagued with how to do this for the past few days… Great Post. Thank You so much.

Reply

John May 14, 2009 at 9:44 pm

Hello Zack
I followed most of your instructions and have created the new ‘your-custom-feed.php’ in my theme’s directory and cut and pasted all the php code from the ‘wp-includes/feed-rss2.php’ into it. That parts all good.

However, I don’t know or understand which wordpress .php file to place your ‘12 lines of code’ functions into, to get this custom rss feed to work.

Could you please describe which wordpress .php files to place your new code functions into thanks. And if relevant, the exact position amongst existing wordpress source code.

Nice post thanks

Reply

Davis Mukasa May 15, 2009 at 10:27 am

Hi Zack,

I’ve been stuck on this for a little while. Have put the code in the functions page. Just wondering how you implement the do_feed hook. Can you please give a little example.

Thanks so much.

Davis.

Reply

John May 16, 2009 at 12:02 am

OK, duh I got it, Thanks – Now I have been looking over the CellPhoneFiesta.com xml file and I see tags like (am very interested in the image tag) etc. I take it theses are your new custom tags?

If so, then sorry to ask such a knob question. But if you don’t mind, how have you defined those new tags in “your-custom-feed.php” code? If you could point me in the right direction on how to build my own custom xml tags in php, or even one or two small examples that would be great.

Cheers

Reply

John May 16, 2009 at 12:08 am

opps, php striped out the source code. I mean I see tags like (“g:id” “g:brand” “g:image_link” (am very interested in the image tag) “g:product_type” etc)
:)

Reply

Dan May 16, 2009 at 3:01 am

Hey Zack,

This is exactly what I’ve been looking for! I’ve also got a little confused with the ‘do_feed_customfeed’. Is this exactly how it should be written in our code?

Also do you know how a custom feed will affect integration with Feedburner? If not, no worries.

Thanks

Reply

Ali May 18, 2009 at 9:53 am

Thank you for sharing this info..

Reply

Alberto Téllez July 2, 2009 at 2:53 am

You rock!

Reply

B-BOY July 24, 2009 at 4:28 pm

First of all, thanks for the code Zack!
Secondly, I see myself as an experienced Wordpress user with a solid knowledge of PHP, however – even I struggled to find out where the code should go. Especially the placement of rewrite code was a real headache.

So, to help out others – I am sharing with you what I did with the code:
Put the first part of the code in functions.php, located in your theme’s folder.

The second part (rewrite code) worked for me, only after putting it into wp-includes/rewrite.php on line 195.
Hope this helps!

Reply

Zack Katz July 26, 2009 at 5:02 pm

In WordPress installations, you should not edit anything outside of the wp-content folder; that way, when you upgrade, you don’t need to worry about overwriting all of your customization! Both parts of the code (IIRC) should go in the functions.php file.

Reply

B-BOY August 2, 2009 at 8:32 pm

Hi Zack,

thanks for the quick reply. I understand changing anything outside the wp-content folder would possibly mess up a future upgrade. However, placing both parts of your code into functions.php didn’t do the trick for me. The rewrite function just doesn’t work.

I’m using Wordpress 2.8.
Would you mind telling me where in functions.php these two excerpts should go (line #)?
Thanks in advance.

Reply

kenwooi August 5, 2009 at 8:09 pm

hey zack! thanks for the explanation! i have a question here and i hope you could help me.

previously i bought a domain for my Blogspot blog and when people link me up or follow my blog, the default Blogger feed link was kenwooi.com/feeds/posts/default..

however, recently i have migrated to a a self-hosted wordpress. therefore i had to unpark my domain from Blogspot and redirect it to the new webhost.. and wordpress has default feed links such as kenwooi.com/feed and kenwooi.com/feed/rss..

since i unparked my domain from Blogspot, the default feed link for it has changed back to kenwooi.blogspot.com/feeds/posts/default.. and since many of them “followed” my blog when i had the domain in Blogspot, they wont receive updates anymore..

so is it possible to create a custom feed link for my wordpress? i would like to create a feed link that ends with /feeds/posts/default so that it can update the people..

is it possible by following this methods you’ve mention? sorry if im asking silly questions coz im not very familiar with this matter.. thank you in advance! =)

Reply

Zack Katz August 5, 2009 at 8:34 pm

Hi,
I am guessing this will work. In the custom_feed_rewrite() function, add another line with
'feeds/posts/default/(.+)' => 'index.php?feed=' . $wp_rewrite->preg_index(1)

That might work. Let me know!

Reply

kenwooi August 6, 2009 at 6:37 am

thanks! but i dont get the guide.. where should i insert the code to? where do i add the code for function? im not good in php and all these.. hopefully there is a more detailed step-by-step guide.. it would really help amateurs like me! =)

Reply

kenwooi August 6, 2009 at 7:19 am

okay i know where it is saved already, after reading some of the comments down here!

Reply

kenwooi August 6, 2009 at 7:31 am

1) i added create_my_customfeed and custom_feed_rewrite in the fucntions.php inside my theme folder.

2) i used ‘feeds/posts/default/(.+)’ => ‘index.php?feed=’ . $wp_rewrite->preg_index(1) inside the custom_feed_rewrite function.

3) then i just copy, paste the content from wp-includes/feed-rss2.php and paste it in your-custom-feed.php in my theme folder.

4) It doesn’t work. I tried acccess /feeds/posts/default but it shows “The resource requested could not be found on this server!”

Questions:

1) Any mistakes that i’ve done?

2) What do you mean by — // Make sure to have ‘do_feed_customfeed’ — in the create_my_customfeed function? is it another function? from the guide above, i dont seem to see anything about inserting or making-sure-i-have “do_feed_customfeed” function/code/etc.

3) Do i need to modify the your-custom-feed.php file? Or just copy,paste from feed-rss2.php will do?

Reply

Hunter September 24, 2009 at 5:48 pm

Zach, this doesn’t seem to work for wordpress 2.8. I’m quite comfortable with custom-coding wordpress and php, so I tried to follow the logic, but it would seem that something has changed in the way WP does feeds in the latest version.

This is the error I get after following the above instructions:
“ERROR: customfeed is not a valid feed template”

Any ideas? I don’t care so much about rewriting the URL, just the custom feed template capability.

Reply

Bernardo Farah September 30, 2009 at 11:44 pm

Hi Zack,
I implemented everything you said almost perfectly (I’m using it as a feed on my website through simplepie). Only thing that is not working is the rewrite.

It works as ?feed=customfeed, but not as /feed/customfeed or /customfeed.xml

Is there anything I need to rewrite in what you wrote above? Or is my /%category_name%/%post_name%/ maybe interfering with it?

Cheers!

Reply

Debbie January 24, 2010 at 5:08 am

Has this been answered? I am having the same problem. I want the xml file.

Only thing that is not working is the rewrite.
It works as ?feed=customfeed, but not as /feed/customfeed or /customfeed.xml

Thx

Reply

Andrew Koop October 8, 2009 at 2:03 pm

I’m getting a 404 error when using feed/customfeed and the following when using ?feed=customfeed.

Warning: require_once(/home/.manka/uitsnews/uitsnews.koop.ws/wp-content/themes/uitsnewsyour-custom-feed.php) [function.require-once]: failed to open stream: No such file or directory in /home/.manka/uitsnews/uitsnews.koop.ws/wp-includes/theme.php on line 843

Seems like I’m missing a slash between my theme name and the custom-feed file. Any ideas on how I could fix that? Thanks!

Reply

Andrew Koop October 8, 2009 at 2:06 pm

sorry for the multiple postings — didn’t see it jumped up there. :)

Reply

Andrew Koop October 8, 2009 at 2:14 pm

Also, I was a bit confused by this line w/in the 12 lines of functions code:

// Make sure to have 'do_feed_customfeed'

Is that a comment on what the function is doing or does that mean I need to add a reference to do_feed_customfeed in my your-custom-feed file? Thanks!

Reply

Zack Katz October 8, 2009 at 9:17 pm

That means that if you renamed your feed, you must rename the function to match in the add_action function.

Zack Katz October 8, 2009 at 8:52 pm

Hi Andrew, add the slash to the following code load_template( TEMPLATEPATH . 'your-custom-feed.php'); to make it TEMPLATEPATH . '/your-custom-feed.php'

Reply

Andrew Koop October 8, 2009 at 11:52 pm

sweet. thanks!

Reply

Andrew Koop October 9, 2009 at 8:19 pm

got this working and it’s awesome. i need to implement it in a wp 2.0.2 install. can you think of any reason why it might not work in that iteration? thanks again.

Reply

Giraldi Maggio January 13, 2010 at 5:47 pm

I experienced the same problem initially.
Maybe you should update this missing slash issue in your tutorial above so that people don’t get confused.
Cheers.

Reply

Andrew Koop October 8, 2009 at 2:04 pm

I’m getting a 404 error when using feed/customfeed and the following when using ?feed=customfeed.

Warning: require_once(/home/.manka/uitsnews/uitsnews.koop.ws/wp-content/themes/uitsnewsyour-custom-feed.php) [function.require-once]: failed to open stream: No such file or directory in /home/.manka/uitsnews/uitsnews.koop.ws/wp-includes/theme.php on line 843

Seems like I’m missing a slash between my theme name and the custom-feed file. Any ideas on how I could fix that? Thanks!

Reply

Andrew Koop October 8, 2009 at 2:05 pm

I’m getting a 404 error when using feed/customfeed (I use pretty permalinks) and a failed to open stream message when using ?feed=customfeed.

Seems like I’m missing a slash between my theme name (based upon the failed to open stream error message) and the custom-feed file. Any ideas on how I could fix that? Thanks!

Reply

David Ruzicka October 9, 2009 at 7:55 pm

At least for WP 2.8.4 the correct code for this to work is:


load_template( TEMPLATEPATH . '/your-custom-feed.php');

Notice the slash.

Reply

Pete October 24, 2009 at 12:29 pm

Hey Zack, great little script here. I followed your code and template instructions (thanks for that, btw). The feed url is redirecting to the template okay, but the google-base-feed.php template is bringing up this error:

Fatal error: Call to undefined function get_option() in /home1/boredaga/public_html/bnbyowner/wp-content/themes/agent_30/google-base-feed.php on line 25

I notice that if I take out the get_option() coding, I still get an error on the have_posts() code.

Any thoughts? Are their any pieces of your template that are specific to your database?

Reply

Peter Host October 26, 2009 at 11:00 am

Hi,

Is there a chance to you to create a step-by-step guide to implement it?
Specially to people with short knowledge of WP.

Thanks in advance,
P. Host

Reply

Milos December 17, 2009 at 1:49 pm

Hi Zack,
thanks for this peace of code. However it doesnt function like it should for me… I can only access feed with ?feed=customfeed and not /customfeed. I checked the template path and it’s ok.
I want to use custom feed template for my categories e.g. http://mysite.com/category-1/customfeed
Any help would be much appreciated.
Cheers!
M

Reply

djerba February 21, 2010 at 5:17 pm

Nice tutorial :)
I’m trying to include post’s thumbnails in RSS feeds.
So I think it’s possible with MySql query (post_type and post_parent)
 
Thank you :)

Reply

Zack Katz April 2, 2009 at 11:22 am

Hey Kim,
This is not for custom fields really…it’s to create a new feed type, like RSS/RSS2/Atom. You can then pull in custom field values into your feed.

Is what you want a URL like http://mysite.com/customfield/? If so, there are two plugins you should take a look at:

Custom Taxonomies Plugin — Does some things 2.8 will do for Custom Fields, but now and possibly easier
More Fields — A great plugin that gives a better Custom Fields administration box that also provides you with the URL rewriting.

Does that help?

Reply

Zack Katz April 9, 2009 at 4:54 pm

Thanks, I’m glad you found it helpful.

Reply

Zack Katz May 14, 2009 at 9:54 pm

Hi John,
If your theme has a functions.php file, add it there. If not, create a file in your theme’s folder called functions.php, and…well…add it there!

Let me know if that helps.

Reply

Zack Katz May 19, 2009 at 9:38 am

I will post the template shortly, so you can see how I did that. It’s all based on custom fields, then formatted for Google Base.

Reply

Zack Katz June 4, 2009 at 9:51 am

@John – It’s been a while, but here’s a link to the code that I used to generate the Google base feed:
http://www.katzwebdesign.net/downloads/google-base-feed.php.txt

Reply

Zack Katz May 19, 2009 at 9:42 am

Hi Dan,
The do_feed_customfeed code is what changes the name of your feed itself. If you had the following code add_action('do_feed_crazyfeedname', 'create_my_customfeed', 10, 1);, the feed would be called crazyfeedname, which would be accessible at index.php?feed=crazyfeedname and crazyfeedname.xml.

Reply

Leave a Comment

Previous post:

Next post: