Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question
    
I usually use a simple plugin to redirect or do it in .htaccess to the same effect. Is that not acceptable? –  Rev. Voodoo Feb 16 '12 at 12:38
    
Have you tried the Feedburner plugin? –  m0r7if3r Feb 16 '12 at 12:41
    
I guess it is acceptable to use .htaccess, but it makes it harder for someone with less knowledge to do it if they use my theme. I will check out the plugins :) –  andeersg Feb 16 '12 at 12:46
    
Ah, I see what you are doing. IMO, I don't include feedburner options within a theme. I leave that up to the end user. Is this still valid? justintadlock.com/archives/2008/12/15/… –  Rev. Voodoo Feb 16 '12 at 13:50
    
I see your point, but i think it's nice to have the opportunity to easily add feedburner. But i want it to be optional. I will check your link :) –  andeersg Feb 16 '12 at 15:49
add comment

1 Answer

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

share|improve this answer
    
Thank you for a good explanation. I will try your solution :) –  andeersg Feb 16 '12 at 21:23
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.