0

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 ()

  • 1
    Please search the site. This has been answered sooooo many times before... – kaiser Dec 1 '12 at 13:42
2

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
}
  • 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. – fuxia Dec 1 '12 at 13:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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