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?
I couldnt find a better method other than:
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,
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.
I know this post is almost a decade now, but I will just put it out there since I found it useful.
This is a leaf from what the guys have posted previously. Just wrapping it up together. I hope this helps anyone needing it.