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.

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

share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

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;
        }
    }
}
share|improve this answer
    
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 :) –  BandonRandon Apr 6 '11 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 Apr 6 '11 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. –  Punkis Jun 19 '11 at 1:05
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.