Custom RSS feeds in WordPress

I use FeedBurner to track all my feed subscribers, but sometimes I want to provide a different feed to other people (without my feed signature for instance, or using excerpts instead of full feeds).

Now when you use FeedBurner's Feedsmith plugin, this'll redirect all of your normal feeds to FeedBurner, and that's a smart thing to do: you don't want to give those custom feeds to people they aren't meant for.

There's an easy solution though, just follow these steps:

  1. create a custom page template within your theme that serves out the feed (code below)
  2. create a new page (which can be blank), with a secret URL
  3. use your newly created "Custom Feed" template for that page
  4. open the secret URL in your browser and voila: your custom feed is ready

The template should look something like this:

<?php
/*
Template Name: Custom Feed
*/
 
$numposts = 5;
 
function yoast_rss_date( $timestamp = null ) {
  $timestamp = ($timestamp==null) ? time() : $timestamp;
  echo date(DATE_RSS, $timestamp);
}
 
function yoast_rss_text_limit($string, $length, $replacer = '...') { 
  $string = strip_tags($string);
  if(strlen($string) > $length) 
    return (preg_match('/^(.*)\W.*$/', substr($string, 0, $length+1), $matches) ? $matches[1] : substr($string, 0, $length)) . $replacer;   
  return $string; 
}
 
$posts = query_posts('showposts='.$numposts);
 
$lastpost = $numposts - 1;
 
header("Content-Type: application/rss+xml; charset=UTF-8");
echo '<?xml version="1.0"?>';
?><rss version="2.0">
<channel>
  <title>Yoast E-mail Update</title>
  <link>http://yoast.com/</link>
  <description>The latest blog posts from Yoast.com.</description>
  <language>en-us</language>
  <pubDate><?php yoast_rss_date( strtotime($ps[$lastpost]->post_date_gmt) ); ?></pubDate>
  <lastBuildDate><?php yoast_rss_date( strtotime($ps[$lastpost]->post_date_gmt) ); ?></lastBuildDate>
  <managingEditor>joost@yoast.com</managingEditor>
<?php foreach ($posts as $post) { ?>
  <item>
    <title><?php echo get_the_title($post->ID); ?></title>
    <link><?php echo get_permalink($post->ID); ?></link>
    <description><?php echo '<![CDATA['.yoast_rss_text_limit($post->post_content, 500).'<br/><br/>Keep on reading: <a href="'.get_permalink($post->ID).'">'.get_the_title($post->ID).'</a>'.']]>';  ?></description>
    <pubDate><?php yoast_rss_date( strtotime($post->post_date_gmt) ); ?></pubDate>
    <guid><?php echo get_permalink($post->ID); ?></guid>
  </item>
<?php } ?>
</channel>
</rss>

18 Responses to “Custom RSS feeds in WordPress”

  1. Cool! This is a creative trick. With this method, we can offer custom feed for categories, tags, ... Thank you very much!

  2. Thanx for this feed / page template, very useful for producing custom feeds.

  3. Please bear with me...I have some questions that may seem silly to you...
    1. Can I use this as a newsletter? I want something to go out to feedburner subscribers but don't want to post it.
    2. "open the secret URL in your browser and voila: your custom feed is ready"
    What does that mean? When I open the URL, who does the feed go to? Does this mean whatever I post in this template will go the feedburner subscribers?

  4. My website is Doctors View. How Can I create feedburner account and how can I place it to my WP blog? please help me.

  5. Hey, dig this post a bunch, but out of curiosity, how would your recommend best handling a RSS feed that uses a lot of custom fields? I have a solution that works (mostly) based on a hacked WordPress plugin due to the fact that RSS support for custom fields isn't right out of the box for RSS feeds, but it's not perfect and once in a while the fields show up in the wrong order:

    http://feeds.feedburner.com/shortformblog/feed

    This method would be very useful for me because I could split the feeds up by sections or have a feed specifically for posts I'd like to feature on Twitter or Facebook.

    If you have any thoughts, I'd love to hear them.

  6. I've been searching for this info lately and finally found it. Haha.. Thanks for this useful info, I can now customize my own feed.

  7. Is there a way to limit the feed to ONLY showing 100 words, not characters but words?

    I would love to be able to do that.

  8. Great stuff. I've been looking to create a custom feed. Thanks!

  9. could anyone say wat makes this one different from default RSS feeds? :S

  10. There is a somewhat nicer plugin-based way to do this sort of thing.

    function my_feed() {
    // code to output the feed goes here
    }
    add_feed('feedname', 'my_feed');

    Now you can call that feed up on your site using http://example.com/feed/feedname. That's all there is to it.

  11. Hi - Thanks very much for this. Really helpful. I'm wondering if you could help me please with something. It seems possible to restrict the custom feed to a single category and to make a call to a custom field ("events date") of those posts, in a way which would replace the ordinary "pub date" with, in fact, the custom field date.

    Do you think this is doable? I'd gladly donate if you were able to help me out with this.

    Thanks in advance for any assistance you can give

    Richard

  12. Nice! Very useful indeed, just implemented it for one of our blogs :)

  13. Thanks a lot!
    One suggestion: you can use parameter to create filtered RSS feeds (tags, categories, authors) using templates tags http://codex.wordpress.org/Template_Tags/query_posts.
    One question: how would feed the entire post with images and HTML layout (bold, etc.)

  14. I found a solution to add a automatic thumbail to my RSS feed to send a daily newsletter via MailShimp. Here the code of my RSS feed: http://www.wordpress-fr.net/support/viewtopic.php?pid=158437#p158437 (2d message, even you don't speak french, you'll understand my code !)

  15. Thank you very much very nice article
    Great information! Very useful for me. Thanks a lot.

  16. Dear sir, I have try the script but why when I access the page there no rss content in it ? (*sorry for my bad english)

  17. is any wordpress plugin limit words in the /feed ? because feedburner send by emails all of post
    not a excerpt

Comments closed, if you feel you have something to say:
drop me a line.

4 Trackbacks to “Custom RSS feeds in WordPress”

  1. EG-Link: revue de blogs | Emmanuel GEORJON - Mon, June 15th, 2009 at 07:17
  2. links for 2009-06-16 | Bakkel dot com - Wed, June 17th, 2009 at 01:13
  3. Custom feed in Wordpress | Bakkel dot com - Sun, July 26th, 2009 at 10:47