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


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.



 
 

Related Links:

Content

Cached Text (at the time of saving)


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?
This entry was posted in Tutorial, WordPress and tagged Custom feed, Custom Feed Tutorial, Feed Rewrite, Feed Template, generate_rewrite_rules, Google Base Feed, RSS Feed, WordPress Custom Feed, WordPress Rewrite, WordPress RSS Feed. Bookmark the permalink.