Question

I have a website where I let people subscribe. I would like to only show the author page for actual authors who have written a post. I wrote this code that checks for post the problem is I can't use a wp_redirect or include a template that uses it because then I get everyones favorite "cannot redifine headers header" message. I could display a "User has no post message but I think redirecting them to the main author page is a better option.

if ( is_author() ) : ?>

    <?php $id = get_query_var( 'author' );

    $post_count = get_usernumposts($id);
    if($post_count <= 0){ 
                 //This line could also be wp_redirect 
                 include( STYLESHEETPATH .'/author-redirect.php');
                 exit;
      }
endif;?>

Thanks

Was it helpful?

Solution

You can do this at an earlier moment by hooking into the right action, like template_redirect, which fires right before the template will be displayed.

add_action( 'template_redirect', 'wpse14047_template_redirect' );
function wpse14047_template_redirect()
{
    if ( is_author() ) {
        $id = get_query_var( 'author' );
        // get_usernumposts() is deprecated since 3.0
        $post_count = count_user_posts( $id );
        if ( $post_count <= 0 ) { 
            //This line could also be wp_redirect 
            include( STYLESHEETPATH .'/author-redirect.php' );
            exit;
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top