Tutorial WordPress

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

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 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 It took custom fields from each post and created a compliant feed.  Check out the Google Base feed that uses this code.

Any questions?

84 replies on “How to Create a Custom RSS Feed in WordPress in 12 Lines of Code”

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? 😉


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.


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

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.


OK, duh I got it, Thanks – Now I have been looking over the 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.


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)


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.


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!

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.

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.

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

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 and

since i unparked my domain from Blogspot, the default feed link for it has changed back to 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! =)

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!

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! =)

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!”


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?

[…] possible de créer un flux RSS personnalisé par inscription dans un annuaire (voir l’article How to Create a Custom RSS Feed in WordPress in 12 Lines of Code ) ou de supprimer la description de l’article dans le flux. Cela peut résoudre en partie le […]

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.

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?


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


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

Warning: require_once(/home/.manka/uitsnews/ [function.require-once]: failed to open stream: No such file or directory in /home/.manka/uitsnews/ 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!

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!

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

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

Warning: require_once(/home/.manka/uitsnews/ [function.require-once]: failed to open stream: No such file or directory in /home/.manka/uitsnews/ 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!

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!

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.

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?


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

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.
Any help would be much appreciated.

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 🙂

@Anabelle: I forgot to add .htaccess rules to force .txt files to render as text, even if they’re php…Anway, try the link again and it should work.

Thanks for the great post Zach.  I think this is a good reference.
I was trying to get it working in WP 2.9 and was having several problems.
The first problem was that when I was working on my feed and customizing it, I was getting an HTTP status 304 returned, which meant my browser just kept loading the same cached page.  I was able to circumvent that with the following:

function spoof_lastpostmodified($lastpostmodified, $timezone){
// I added this function in to spoof the lastpostmodified date feeds
// because WP was caching the feed (status 304 - see line 354 in wp-includes/classes.php)
// In my testing, I needed that caching not to happen
global $wp;
if (!empty($wp->query_vars['feed'])){
$lastpostmodified = date("Y-m-d H:i:s");  // Now
return $lastpostmodified;

The second problem I was having was getting feed/customfeed working (like other posters, /?feed=customfeed was working fine).  To get around that, I added the following:

function custom_feed_rewrite_rule($rules){
$feed_rules = array(
'feed/customfeed' => 'index.php?feed=customfeed',
'customfeed' => 'index.php?feed=customfeed',
'customfeed.xml' => 'index.php?feed=customfeed'
$rules = $feed_rules + $rules;
return $rules;

Adding the transient_rewrite_rules filter means that it works even if the rewrite_rules have been cached (i.e. transient), and the rewrite_rules_array filter makes it work when those rules haven’t been cached.
Adding the above code to my theme/functions.php file corrected the URL rewrite issue that many posters have cited as well as the problem of the feed being browser cached (due to the 304 status).
Hope this helps someone else.

Hm, I can’t get the rewrite rules to work on wp 2.9.2, and I also want to find out how to get feed params to work. is getting automatically sent to where “toppsak” is the category name of id 305. Returns as 404. works
I’ve tried several solutions, but the only thing I’ve figured out thus for is an error on /wp-admin/options-permalink.php if I use
Fatal error: Unsupported operand types in /hsphere/local/home/nrkbetasite/ on line 97
Line 97 contains the line
$wp_rewrite->rules = $feed_rules + $wp_rewrite->rules;

Hi, to get this to to work I had to add:
function flushRules(){
global $wp_rewrite;

then works like a charm! Hope this helps someone.


Hi, although this works, I think it will flush your rewrite rules every time the filter hook register_activation_hook is called. I would rather flush it once after creating this rule, for that, you simply need to visit the admin page: Settings > Permalinks
It did the trick for me!

@estepix, that’s not correct. Any code called by `register_activation_hook` is triggered when a plugin is activated. If you add that code, every time a plugin is activated, the rules will be flushed.

Great post. Got the basics working. Any suggestions on how I would change the number of posts displayed in my custom feed, but not in the normal feeds?


I want to make my custom feed, but I don’t understand were I have to put the code above, nor where do I have to make sure that I have ‘do_feed_customfeed’, nor where do I have to re-write my own feed. Could you please especify the files names? Sorry, I’m new in this kind of stuff.


Hey Zack,

First time visiting your site and this has been a lifesaver tutorial! Been trying to sync WordPress with google base (now Google Merchant Center) and haven’t been able to automate it.

My question is, how do you make the RSS feed display all the products we have listed, not just the recent ones? So Google Merchant Center doesn’t lose some of our older products, but which are still in our store (just not “published” recently).

Also, do you know how to get the actual RSS file in .xml format to use with Google Merchant Center isntead of it just showing as ?

Again, thanks so much for keeping this thread running

I am having trouble getting this to work. This is what i did.
1. Added create_my_customfeed and the add_action to my function.php in the theme folder
2. added custom_feed_rewrite and the add_filter to my theme folder.
3. Duplicated feed-rss2.php and renamed it your-custom-feed.php and put it in my theme folder.

now i tried to navigate to
and i got nothing
can anyone help


i got the feed to work…
make sure
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
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

now how to get the rewrite to work…this i can’t get working

Also I’ve found if you’re trying to use this in a child theme, TEMPLATEPATH points to the parent theme, which doesn’t have the file and causes an error in WP. Use this code instead:

function create_my_customfeed() {
locate_template( array( ‘/your-custom-feed.php’ ), true ); // You’ll create a your-custom-feed.php file in your theme’s directory

And I couldn’t get the rewrite to work either.

Great tutorial. I didn’t look at the plugin code but I’m guessing this is essentially what the “Feed Wrangler” plugin does for you – then you just create your own feed template. For those having trouble Feed Wrangler might be worth a look.

please help me here .. i am using Auto excerpt plugin for only home page displaying posts to show only post excerpt… but this plugin affecting my feed … when i write some new article then in feed it only display excerpt which i don’t want .. how to avoid it .. i want to use this plugin .. or any alternative… please help me 

Thanks Zack. One could use get_template_directory() in the place of TEMPLATEPATH for more stable results

Comments are closed.