Creating custom feeds in WordPress

There are many reasons why you would want to create a custom feed in WordPress. For example, you might have a 3rd party service that want to consume a feed in a non-standard format.

Making use of the add_feed() function, you can easily set it up, with a little bit of code. Unfortunately, there is no proper documentation for this in the WordPress codex, however, I will attempt to explain how it works. This will make it easy to have a custom feed, something like: example.com/feed=custom-rss

About add_feed
This function will take two parameters: Feed Name, and Callback Function.
Feed Name is the unique identifier of the custom feed.
The callback function will handle the feed template, and will have access to Loop.

Timing matters
The add_feed function MUST be called right after WordPress is completely initialized. In my tests, I found it to work best in the init action hook. Add something like the following code to your functions.php file:


Creating the template
At this point, all you still need to do is to create the custom template. The easiest way to go about this is to copy the wordpress/wp-includes/feed-rss2.php template to your theme directory. Rename it to custom_rss_feed.php and start modifying the file to your needs. All of the usual WordPress functions will still work, because you are still in the Loop.

Afterthought
You can then take it a step further and create, rewrite rules so that you don’t need the query string. Instead of having feed=custom-rss you can have something like feed/custom-rss/
To make the rewrite rules work, you can make use of $wp_rewrite.