Category Archives: WordPress

Create a custom feed in WordPress

Recently I was faced with the task to create various formats of the WordPress feed. My initial thought was to create a new “page” and use that as the feed. So you would call something like domain.com/custom-feed/ Then I found the add_feed() function: I could not find proper documentation for the function in the Codex, but here is an attempt to explain how it works. This make is easier to call domain.com/feed=custom-rss to get the custom RSS feed.

About add_feed
This function takes two parameters: Feed Name, and Callback Function. The callback function will handle the feed template, and will have access to Loop.

Timing matters The add_feed function MUST be called after WordPress is completely initialized. In my tests. I found it to work in the init action hook.

The code

function custom_rss_feed()
{
     add_feed('custom-rss', 'custom_rss_feed_template');
}
add_action('init', 'custom_rss_feed');

function custom_rss_feed_template()
{
     load_template( TEMPLATEPATH . 'custom_rss_feed.php');
}

The code explained
First, we add an action to call the custom rss function during the init. Then, we define the new custom rss feed using add_feed. This feed will then give use a ‘custom-rss’ feed. The custom_rss_feed_template is called when feed=custom-rss is specified. The custom_rss_feed_template function calls your template 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 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 feed=custom-rss but instead something like feed/custom-rss/

Share:

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.

Share: