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>

Tags:


Yoast.com runs on the Genesis Framework

Genesis theme frameworkThe Genesis Framework empowers you to quickly and easily build incredible websites with WordPress. Whether you're a novice or advanced developer, Genesis provides you with the secure and search-engine-optimized foundation that takes WordPress to places you never thought it could go.

Read our Genesis review or get Genesis now!

22 Responses

  1. FreewareMatterBy FreewareMatter on 9 June, 2009

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

  2. Jasja ter HorstBy Jasja ter Horst on 10 June, 2009

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

  3. KaushikBy Kaushik on 10 June, 2009

    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. Dr. Md. Masud KabirBy Dr. Md. Masud Kabir on 11 June, 2009

    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. Ernie SmithBy Ernie Smith on 12 June, 2009

    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. IJ StylesBy IJ Styles on 13 June, 2009

    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. MatthewBy Matthew on 15 June, 2009

    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. 123 TweakBy 123 Tweak on 16 June, 2009

    Great tips
    Thanks

  9. StevoBy Stevo on 16 June, 2009

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

  10. fikretBy fikret on 17 June, 2009

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

  11. OttoBy Otto on 17 June, 2009

    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.

  12. RichardBy Richard on 27 June, 2009

    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

  13. Ruud KokBy Ruud Kok on 25 August, 2009

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

  14. AntoineBy Antoine on 1 October, 2009

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

  15. AntoineBy Antoine on 8 October, 2009

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

  16. sexy costumesBy sexy costumes on 26 October, 2009

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

  17. kinta mahadjiBy kinta mahadji on 28 November, 2009

    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)

  18. TechadictosBy Techadictos on 28 November, 2009

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

Trackbacks