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

Related posts:

  1. Fixing the Interspire Google Base Feed
  2. Add a Store Catalog XML Sitemap to the Shopp Plugin

This entry was posted in Tutorial, WordPress and tagged , , , , , , , , , . Bookmark the permalink.
  • kwbridge

    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!

  • kwbridge

    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!

  • http://anthonylrivera.net anthonylrivera

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

  • Pingback: Weave Snatch » Blog Archive » haiku update!

  • http://www.moneymaker-guide.com/ John

    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

  • Davis Mukasa

    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.

  • http://www.moneymaker-guide.com/ John

    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

  • http://www.moneymaker-guide.com/ John

    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)
    :)

  • Dan

    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

  • http://www.agck.com Ali

    Thank you for sharing this info..

  • Alberto Téllez

    You rock!

  • B-BOY

    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!

    • http://www.seodenver.com Zack Katz

      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.

      • B-BOY

        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.

  • http://kenwooi.com kenwooi

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

    • http://www.seodenver.com Zack Katz

      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!

      • http://kenwooi.com kenwooi

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

      • http://kenwooi.com kenwooi

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

      • http://kenwooi.com kenwooi

        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?

  • Pingback: Liste d’annuaires de flux RSS

  • Hunter

    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.

  • http://berfarah.com Bernardo Farah

    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!

    • http://www.totalguitar.net Debbie

      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

    • http://pbpulse.com Shaun Flagg

      I can’t get the rewrite to work either. Any word on that?

  • http://uitsnews.koop.ws/?feed=customfeed Andrew Koop

    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!

    • http://uitsnews.koop.ws/?feed=customfeed Andrew Koop

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

      • http://uitsnews.koop.ws/?feed=customfeed Andrew Koop

        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!

        • http://www.katzwebservices.com Zack Katz

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

    • http://www.katzwebservices.com Zack Katz

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

      • Andrew Koop

        sweet. thanks!

      • http://uitsnews.koop.ws/?feed=customfeed Andrew Koop

        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.

      • http://dmaggio.carbonmade.com Giraldi Maggio

        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.

  • http://uitsnews.koop.ws/?feed=customfeed Andrew Koop

    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!

  • http://uitsnews.koop.ws/?feed=customfeed Andrew Koop

    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!

  • David Ruzicka

    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.

  • Pete

    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?

  • Peter Host

    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

  • Milos

    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

  • http://jerba.be djerba

    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 :)

  • http://coderamblings.com Anabelle

    Could you provde a new link with the google base feed code, this one is empty
    http://www.katzwebdesign.net/downloads/google-base-feed.php.txt
     

    • http://www.katzwebservices.com Zack Katz

      @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.

  • http://www.topquarkproductions.ca Trevor Mills

    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:
    add_filter('get_lastpostmodified','spoof_lastpostmodified',10,2);

    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:
    add_filter('transient_rewrite_rules','custom_feed_rewrite_rule');
    add_filter('rewrite_rules_array','custom_feed_rewrite_rule');

    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.
     
     

  • thsoto

    Has anyone gotten this to work with feed parameters?

    `http://news.com/?feed=customfeed&cat=4`

  • Pingback: WordPressハッカーズ

  • Morten Skogly

    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.
    http://nrkp3.no/fpbeta/?feed=filmpolitietfeed&cat=305 is getting automatically sent to http://nrkp3.no/fpbeta/category/toppsak/feed/filmpolitietfeed/ where “toppsak” is the category name of id 305. Returns as 404. http://nrkp3.no/fpbeta/?feed=filmpolitietfeed 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
    add_filter(‘rewrite_rules_array’,'custom_feed_rewrite_rule’);
    saying
    Fatal error: Unsupported operand types in /hsphere/local/home/nrkbetasite/nrkp3.no/fpbeta/wp-content/themes/filmpolitiet/functions.php on line 97
    Line 97 contains the line
    $wp_rewrite->rules = $feed_rules + $wp_rewrite->rules;
     
     

  • Mark

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

    add_filter(‘register_activation_hook’,'flushRules’);

    then works like a charm! Hope this helps someone.

    Cheers
    Mark

    • http://www.martyspellerberg.com Marty

      Thanks for this — I needed this to make it work. (My site uses WordPress Network)

  • http://www.triplepundit.com Nick Aster

    Does anyone know how to do this without resorting to php?
    I have been following these instructions:
    http://yoast.com/custom-rss-feeds-wordpress/
    And it works, but I need to feed to be FULL TEXT and have an <author> attribute.  ANy clues?

  • http://www.devsolution.net spam-dev

    Hi Dan,
    thank you.  It very sample.
     
    spam-dev

  • Pingback: Classroom Technology Thoughts (Twitter Weekly Updates) for 2010-06-14 | blog.classroomteacher.ca

  • Tom

    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?

    Thanks
    Tom

  • http://www.extremista.com.ar Sebastian

    Hi,
    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.

    Thanks
    Sebastian

  • http://marketheist.com Jeffrey Lin

    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 http://mysite.com/feed ?

    Again, thanks so much for keeping this thread running

  • Pingback: Overriding Feed Templates in WordPress « Form As Function

  • Pingback: Create a Custom RSS Feed in WordPress

  • Arne Seemann

    Thanks so much for your tutorial. Saved me a ton of work :)

  • http://ilanperez.com Ilan Perez

    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 site.com/category/people/feed/customfeed/
    and i got nothing
    can anyone help

    Ilan

    • http://ilanperez.com Ilan Perez

      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

      is
      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

      • Chris

        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.

  • http://sunriseweb.ca Brad Trivers

    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.

  • http://twitter.com/novamedia Madame K

    bien vu

  • http://rentalpropertydelhi.com rental property in delhi

    thanks buddy for sharing this tips with us….!

  • gigix

    your explanations are veeery confusing.

    • http://www.seodenver.com Zack Katz

      I will be re-writing this article in a week or so – I know it’s hard to understand right now and there are many questions left un-answered!

  • Yylexer

    HI Zack Katz
    This worked for me.
    Thanks.

  • Pingback: where to upload .xml in WP - SitePoint Forums

  • Anonymous

    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 

  • http://webastral.com palvinder

    how to customized rss feed in wordpress?

  • http://www.buy-genericviagra.com/ Viagra Online

     interesting topic for read thanks for it.

  • http://www.seodenver.com Zack Katz

    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?

  • http://www.seodenver.com Zack Katz

    Thanks, I’m glad you found it helpful.

  • http://www.seodenver.com Zack Katz

    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.

  • http://www.seodenver.com Zack Katz

    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.

  • http://www.seodenver.com Zack Katz

    @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

  • http://www.seodenver.com Zack Katz

    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.