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 was wondering which is the correct method to do this and which action hook should i use.

I have custom login/register pages so if the user try to go to a forbidden page and its not logged in i will redirect him to a login page.

Currently on my functions.php i got the following:

/*
*   Restrict non logged users to certain pages
*/

add_action('template_redirect','my_non_logged_redirect');
function my_non_logged_redirect()
{
     if ((is_page('mi-perfil') || is_page('agregar-empresa')) && !is_user_logged_in() )
    {
        wp_redirect( home_url() );
        die();
    }
}   

Im using the right method/hook or should i use another one or a easier one?

share|improve this question
    
Does your posted code work? If not, in what way does it not work, or work differently from what you expected? –  Chip Bennett Aug 27 '12 at 13:22
    
it works fine. Just wanted to know if is proper way or i should use any other hook than template_redirect –  chifliiiii Aug 27 '12 at 14:37
    
"It works fine. Just wanted to know if is proper way or i should use any other hook than template_redirect" Close-voted as not constructive: "As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or specific expertise, but this question will likely solicit debate, arguments, polling, or extended discussion." –  Chip Bennett Aug 27 '12 at 14:49
    
So there is no way to ask if im doing things right? –  chifliiiii Aug 27 '12 at 14:56
    
Place your code as an answer. Reformat your question to ask how to accomplish this. The community can vote up/down your answer and other's possible solutions as well. This will give you the desired feedback within the scope of SE. –  Brian Fegter Aug 31 '12 at 3:59
add comment

3 Answers

The posts/pages which you want to hide from non logged in users can be published as "Private" and it will not be available to any public visitor of the site. This way you don't need to implement any sort of redirection or custom code. You can find more information here.

share|improve this answer
add comment

I just want to say thanks, I was able to use this and do exactly what I hoped changing the line to cover the category and sending people to my login page instead of the home url,

/*
*   Restrict non logged users to certain pages
*/

add_action('template_redirect','my_non_logged_redirect');
function my_non_logged_redirect()
{
     if ((in_category(1) && !is_user_logged_in() ))
    {
        wp_redirect( 'http://mysites.com/loginpage/' );
        die();
    }
}
share|improve this answer
add comment
up vote 0 down vote accepted

I couldnt find a better method other than:

/*
*   Restrict non logged users to certain pages
*/

add_action('template_redirect','my_non_logged_redirect');
function my_non_logged_redirect()
{
     if ((is_page('mi-perfil') || is_page('agregar-empresa')) && !is_user_logged_in() )
    {
        wp_redirect( home_url() );
        die();
    }
} 
share|improve this answer
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.