In my theme i use add_theme_support( 'automatic-feed-links' );
to enable the rss links.
But i want to create an option to use Feedburner for the main rss feed, my problem is that i don't know how i can hook in and change the default rss link?
In my theme i use add_theme_support( 'automatic-feed-links' );
to enable the rss links.
But i want to create an option to use Feedburner for the main rss feed, my problem is that i don't know how i can hook in and change the default rss link?
You can take advantage of the template_redirect
hook and redirect any request to the WordPress feed to a URL that you define.
The most important thing to consider is the case when users are attempting to access the feed itself. In that case, you'd obviously not want to do any redirection.
Here's how you can do that:
function example_redirect_feeds() {
// We only want to redirect if we're accessing the feed.
if(is_feed()) {
// Define the URL's to which we'll redirect...
$feed_url = 'YOUR_FEED_REDIRECT_URL';
$comment_feed_url = 'YOUR_COMMENT_FEED_REDIRECT_URL';
global $feed, $withcomments;
// If the user is requesting to access the comment feed, redirect...
if($feed == 'comments-rss2' || $withcomments) {
header("Location: " . $comment_feed_url);
die();
// ...otherwise, go ahead and redirect to the feeds you defined above.
} else {
// We need to capture all different feed types
switch($feed) {
case 'feed':
case 'rdf':
case 'rss':
case 'rss2':
case 'atom':
header("Location: " . $feed_url);
die();
break;
default:
break;
} // end switch/case
} // end if/else
} // end if
} // end example_redirect_feeds
add_action('template_redirect', 'example_redirect_feeds');
If you're planning to redirect feeds for comment streams, too then you'll need to add that to this function, as well.
Note that I'm unsure if using die
is the best practice here - it gets the job done, but "feels" a bit weak (though it is better than wp_die
since that function is designed to actually return an error message - not just halt execution).