Custom WordPress RSS feeds and Feedburner

A great deal of web content is accessible through syndication feeds. RSS, the de facto standard, is often the singular path to publishings of an author’s blog. Despite rumblings of its demise, RSS remains an essential method of news consumption. As such, a feed should strive to provide the clearest, cleanest presentation of content possible outside of the feed’s source website.

I recently introduced a second stream of content to this site, Snapshots of web content that I find inspiring or informative. The format for presentation of this stream is still under debate, but the current solution mixes the posts into my normal blog stream. When it came to delivering this content to an RSS client, I wanted to ensure that readers weren’t forced to click through to this site first to view the linked content. Additionally, the posts categorized as snapshots should be differentiated in the feed.

Subscriptions for this site, like many others, are tracked through Feedburner. My goal was to create a custom RSS feed to accommodate a different type of content post which would overwrite the default WordPress feed, then track the custom feed with Feedburner.

This requires three steps.

WordPress

Automation

With the release of version 3.0, WordPress has supported a small but very helpful function for handling the output of RSS feeds. Previously, all links had to be added manually in the header section of the theme templates. The new function automates the entire process; on an index page it will create a feed for all posts and a feed for all comments; on an individual post, the two global feeds with the addition of a comments feed relevant to the current post. To enable automation, first remove all existing RSS links from the theme header. The function will not work otherwise. Then, add the following to functions.php:

add_theme_support( 'automatic-feed-links' );

If your theme requires backwards compatibility with WordPress before 3.0, have a look at Jeremy Clark‘s solution.

Building a custom feed

The WordPress documentation has a list of recommended methods for creating custom RSS feeds. I chose to build my feed as a custom theme template, based on notes from Zack Katz.

Set a function to load your custom PHP template.
The name of my custom feed template is, quite originally, feed.php and located at the root of my active theme. All mentions of honeycomb are unique, required values. Change the name to something appropriate to your site. Honeycomb is in reference to the name of my theme.

function honeycomb_rss() {
	load_template( TEMPLATEPATH . '/feed.php');
}
add_action('do_feed_honeycomb', 'honeycomb_rss', 10, 1);

Katz’s tutorial also provides a function to manage custom permalinks so that links to /?feed=rss will work as well as /feed/rss/ for custom feeds.

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');

Resaving your existing permalink settings in WordPress->Settings->Permalinks will activate the new rules.

Create a custom feed template
In the function above, the custom feed template is located at /feed.php (eg wp-content/themes/honeycomb/feed.php). Create a new file with your chose template name at this location, and drop the entirety of /wp-includes/feed-rss2.php inside.

This is your custom feed, and will reflect any changes made to the custom template.

The default WordPress posts feed is located at http://www.withoutnations.com/writing/feed/ (alternatively, http://www.withoutnations.com/writing/?feed=rss2). The custom feed noted above is accessible at http://www.withoutnations.com/writing/feed/honeycomb/ and http://www.withoutnations.com/writing/?feed=honeycomb.

Feedburner

The next step is to update Feedburner with the custom address. Select “Edit Feed Details…” from any of the analytics tabs at Feedburner to edit the feed configuration. As we are replacing the default WordPress feed with our custom file, the source that Feedburner looks to for analysis must be changed. In the settings pane, I replaced Original feed http://www.withoutnations.com/writing/feed/ with http://www.withoutnations.com/writing/feed/honeycomb/ (the path to the custom feed).

Apache

With the custom feed defined and the Feedburner source set, the last remaining task is to tell WordPress to forward your main feed path to Feedburner, where it can be properly tracked and managed. There are a number of WordPress plugins that can help with this: FD Feedburner and Primary Feedburner are actively maintained by their developers. Google still provides an older version of Feedsmith as well.

The basic function of all of the plugins is to set an Apache rewrite rule that redirects your feed URL to Feedburner. It’s a convenient service if access to your site’s server environment is restricted or you are uncomfortable editing server-side files. However, if this isn’t a worry, I suggest setting the rewrite rule yourself in .htaccess. It’s straightforward, dependable and does not rely on third-party plugins which are prone to compatibility issues and bugs.

In a not-so-recent article, Jeff Starr at Perishable Press provides a few simple and efficient .htacess rules to achieve WordPress to Feedburner redirects. His suggestions include both post and comment feed redirects. Have a look through his article if this is something your site requires; I was only interested in creating one single custom post feed and setting it to redirect and hence left WordPress to manage the comment feeds.

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} !^.*(FeedBurner|FeedValidator) [NC] 
RewriteRule ^feed/$ http://feeds.feedburner.com/withoutnations [L,NC,R=302]
</IfModule>

Requests for /feed/ will redirect to Feedburner at http://feeds.feedburner.com/withoutnations. This rule reroutes the main feed but leaves /feed/honeycomb accessible to Feedburner.

If your site is using custom permalinks, it is essential to place the Feedburner rewrite rule above the WordPress permalink rewrites, otherwise WordPress ignores the redirect. Typically, permalink rules will look something like:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

The simplest way to test if the feed is working is to check the feed URL in a browser: /feed/ should immediately redirect to the Feedburner account address (eg http://feeds.feedburner.com/withoutnations).