TapCustomised RSS feeds from WordPress

Customising a WordPress installation is relatively straight forward—you pick a theme and then tweak it to your satisfaction—you can make that as easy or hard as you like depending on your skills and requirements and it is all fairly well documented. It uses a theme system so that any templates that you place in the theme directory override the default ones, This blog, for instance, has modified versions of the index, page and single templates which determine the content and appearance of the post stream, pages and single posts respectively.

Customising the feed contents and appearance is not so easy. The system includes three hooks which allow additional content to be included—In the case of RSS2 feed they are rss2_ns in the RSS header, rss2_head in the channel content and rss2_item in each item section. These are what podcast plugins use to add in the iTunes XML stuff that they need.

There is, however, no mechanism to remove or modify the existing standard content. What it needs is the ability to override the templates with ones of your own. The relevant template here is wp-includes/feed-rss2.php and it is loaded from the function do_feed_rss2 in wp-includes/functions.php. That default routine looks like this

function do_feed_rss2( $for_comments ) {
	if ($for_comments)
		load_template( ABSPATH . WPINC . '/feed-rss2-comments.php');
	else
		load_template( ABSPATH . WPINC . '/rss2-atom.php' );
}

What is needed is this which can be put in your theme’s functions.php

function custom_feed_rss2( $for_comments ) {
	if ( $for_comments ) {
		if ( file_exists(STYLESHEETPATH . '/feed-rss2-comments.php'))
			load_template( STYLESHEETPATH . '/feed-rss2-comments.php' );
		else
			load_template( ABSPATH . WPINC . '/feed-rss2-comments.php' );
	}
	else {
		if ( file_exists(STYLESHEETPATH . '/feed-rss2.php'))
			load_template( STYLESHEETPATH . '/feed-rss2.php' );
		else
			load_template( ABSPATH . WPINC . '/feed-rss2.php' );
	}
}

Then replacing the default routine with the new one using

remove_action('do_feed_rss2', 'do_feed_rss2');
add_action('do_feed_rss2', 'custom_feed_rss2');

then the copies of the templates in your theme directory will be picked up if they exist and defaults used if not. Similar things can be done for the rdf, rss and atom feeds but I don’t use them; note: however, that if you have removed things because you don’t want them revealed to the world then you need to do it on all available formats because they are all available whether you advertise them or not.

What do I use it for? well I want to put the event date into some posts rather than the posting date and also I have a need to suppress comment feeds altogether on one site.

4 Responses to “Customised RSS feeds from WordPress”

^ Top