Friday, 9 September 2011

How to Create a Custom RSS Feed in WordPress

Custom RSS feed  

Add below code to your function.php file.

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'

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

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!

 

No comments:

Post a Comment