Halloween Sale 15% off Ends in See the plans
PHP

Send Feed Readers to Your Feed Service

Redirects visitors and feed readers from your main feed to its address at a feed service such as FeedPress or Follow.it, while the service itself still reads your real feed.

Don’t have WPIDE yet? Install it on your site

Code

// Your feed's address at the feed service (FeedPress, Follow.it, Feedburner ...). Leave empty to turn the redirect off.
$wpide_feed_service_url = '';
// Parts of the service's user agent; these requests get your real feed so the service can read it.
$wpide_feed_service_agents = array( 'feedpress', 'feedburner', 'follow.it', 'feedvalidator' );

add_action( 'template_redirect', function () use ( $wpide_feed_service_url, $wpide_feed_service_agents ) {
    if ( '' === $wpide_feed_service_url || ! is_feed() || is_comment_feed() || is_archive() || is_search() || is_singular() ) {
        return;
    }

    // The service can also read the original feed at /feed/?origin-feed=1.
    if ( isset( $_GET['origin-feed'] ) ) {
        return;
    }

    $agent = isset( $_SERVER['HTTP_USER_AGENT'] ) ? strtolower( sanitize_text_field( wp_unslash( $_SERVER['HTTP_USER_AGENT'] ) ) ) : '';

    foreach ( $wpide_feed_service_agents as $needle ) {
        if ( '' !== $agent && false !== strpos( $agent, strtolower( $needle ) ) ) {
            return;
        }
    }

    // 302 keeps subscribers on your own feed address, so you can switch services later.
    wp_redirect( esc_url_raw( $wpide_feed_service_url ), 302 ); // phpcs:ignore WordPress.Security.SafeRedirect -- external address set by the site owner.
    exit;
}, 1 );

Notes

Off until you fill in the service address. Point the service at /feed/?origin-feed=1 if its user agent is not in the list.

How to add this snippet

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.