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.

How am I blocking certain pages from being viewed by logged out users? I have some pages like a dashboard for example that each user has to display their posts an is found in the top level like mysite.com/dashboard. I made a custom template and assigned it to the page "dashboard" but I need to block all non logged in users and limit to author level and up. If the user doesn't meet the criteria then I need to redirect them to the homepage.

I tried placing this at the top of the template but it returned an error.

get_currentuserinfo();
if (!( is_user_logged_in() && current_user_can( 'publish_posts' )) ) {
  wp_redirect(home_url());
  return false;
}

But this leaves the error..

Warning: Cannot modify header information - headers already sent by ()

share|improve this question
1  
Please search the site. This has been answered sooooo many times before... –  kaiser Dec 1 '12 at 13:42

1 Answer 1

up vote 2 down vote accepted

You have to hook in earlier, I would probably use template_redirect:

add_action( 'template_redirect', 'wpse_74577_redirect' );

function wpse_74577_redirect()
{
    // the code from your question
}
share|improve this answer
    
And use a conditional check for the page inside the function? –  Pollux Khafra Dec 1 '12 at 13:45
1  
Yes, of course. :) is_page( 'dashboard' ) should do it. I don’t know how exactly you built that page, so I left it out. –  toscho Dec 1 '12 at 13:49

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.