Redirect WordPress feeds to FeedBurner and other RSS goodies

Written by:
Posted in: ,
Published: March 4, 2013

Just completed a comprehensive RSS setup for a client, including iTunes Podcasting, cross posting to several social media services, and combining feeds for several post types, and I wanted to share some of the tricks we employed to get everything working smoothly for them. These suggestions were compiled from several blogs and forum posts across the net, but there’s a lot of conflicting information out there so I thought I’d share what worked for us.

.htaccess rules for redirecting built in feeds to FeedBurner

FeedBurner is great service. It gives you some great tools for optimizing your feed, lets you manage multiple feeds from one interface, and (maybe most importantly) gives you a count of your total feed subscribers. If you haven’t been using FeedBurner, don’t worry… you won’t lose any subscribers when you make the switch. As your subscribers check their feeds they’ll be redirected from your blog’s built in feed over to FeedBurner, and you’ll start seeing the statistics for your existing subscribers.

The following snippet should be placed at the top of your .htaccess file (in the root of your FTP directory), above the comment [code]#BEGIN WordPress[/code].
[apache]# Redirect WordPress feeds to feedburner
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} !FeedBurner [NC]
RewriteCond %{HTTP_USER_AGENT} !FeedValidator [NC]
RewriteRule ^feed/?([_0-9a-z-]+)?/?$ http://feeds.feedburner.com/yourfeedtitlehere [R=302,NC,L]
</IfModule>
[/apache]
The important part here is at lines 3 and 4. If you don’t check the [code]HTTP_USER_AGENT[/code], then when FeedBurner goes to update your feed, the update checker will get redirected back to FeedBurner and will never detect any updates.

Include multiple post types in your RSS feed

In your functions.php file:

[code php]

function feed_request($qv) {
if (isset($qv[‘feed’]) &amp;&amp; !isset($qv[‘post_type’]))
$qv[‘post_type’] = array(‘post’, ‘custom-post-type-1’, ‘custom-post-type-2’);
return $qv;
}
add_filter(‘request’, ‘feed_request’);

[/code]
Put whatever post types you’d like in your feed into the array on line 3, but leave “post” in there to make sure posts are still. included.

Include the featured image in your feed

This snippet is helpful for services that republish your feed elsewhere (for example, auto-posting your new posts to Facebook). It will insert the post’s featured image at the top of the feed, which can then be picked up and used as a thumbnail. In your functions.php:

[code php]
function featured_to_rss($content) {
global $post;
if ( has_post_thumbnail( $post->ID ) ){
$content = ” . get_the_post_thumbnail( $post->ID, ‘thumbnail’, array( ‘style’ => ‘float:left; margin:0 15px 15px 0;’ ) ) . ” . $content;
}
return $content;
}
add_filter(‘the_excerpt_rss’, ‘featured_to_rss’);
add_filter(‘the_content_feed’, ‘featured_to_rss’);
[/code]

Replace WordPress’ built-in feed links with links to FeedBurner

This step isn’t necessary if you’re redirecting all traffic to FeedBurner via the .htaccess method, but if you’re unable to edit that file for some reason, or you want to completely hide WordPress’ built in feed links, you can use the following snippet to replace all of the /feed/ URLs on the frontend of your site with links to FeedBurner:
[code php]
function custom_rss_feed( $output, $feed ) {
if ( strpos( $output, ‘comments’ ) )
return $output;

return esc_url( ‘http://feeds.feedburner.com/yourfeedtitlehere/’ );
}
add_action( ‘feed_link’, ‘custom_rss_feed’, 10, 2 );
[/code]