Redirect Non-Logged In Users to Specific Page based on Page Template

Here’s an updated version of the code that successfully redirects users who are not logged in to a specific page when they try and view a tag or category archive or a singular post.

add_action( 'template_redirect', 'wpse_redirect_to_specific_page' );
function wpse_redirect_to_specific_page() {
    // Bail if the user is logged in.
    if ( is_user_logged_in() ) {
        return;
    }

    // Redirect to a special page if the user is trying to access
    // a category or tag archive or a single post.
    if ( is_category() || is_tag() || is_singular( 'post' ) ) {

        // Set the ID of the page to redirect to.
        // Change the ID to the desired page to redirect to.
        $redirect_to_page_id = 1086;

        wp_safe_redirect( get_permalink( $redirect_to_page_id ), 301 );
        exit;
    }
}

Handling custom taxonomies

If you’d like to redirect custom taxonomy archive pages, you could add is_tax() to the conditional statement that wraps the redirect code.

Handling custom post types

If you’d like to do the same for all post type singular pages, you could use is_singular() without passing the post parameter, or for specific post types, pass an array of post type names.

tech