3

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

1 Answer 1

4

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;
        }
    }
}
3
  • Awesome, What's up with that function name I know you don't want to conflict with other plugins but that name just seems illogical :)
    – Brooke.
    Commented Apr 6, 2011 at 6:22
  • @BandonRandon: This is WordPress Stack Exchange question number 14047. I create a plugin every time I test some code, this helps me keep them apart. Sometimes I see a question that contains code with other question numbers in them, which gives me a hint into the thought process of the author. And I hope to once see some wpseXXXX functions in the official plugin repository, that would make me proud :-)
    – Jan Fabry
    Commented Apr 6, 2011 at 6:45
  • Hi there, I was wondering how this code must change to hide a specific author but also all things related, like posts, comments and their author page. I created a new question here wordpress.stackexchange.com/questions/20381/… I thing is a nice-to-have idea.
    – EnexoOnoma
    Commented Jun 19, 2011 at 1:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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