I know that are some answers to my question but i don't know if my problem exists in the other persons who asked for help.
I have this function that helps me to redirect users to login before access any contetnt of my website:
function restrict_access_if_logged_out(){
if (!is_user_logged_in() && !is_home()){
wp_safe_redirect(wp_login_url(get_permalink()));
}
}
add_action( 'wp', 'restrict_access_if_logged_out', 3 );
That function works fine but i have a problem. When somebody is navigated to my home page and click to a link that goes here: 'example.com/about' then my function works great. When somebody he trying to go direct to 'example.com/about' by entering this into browser url then my function doesn't work and he is able to access my website. What i have do wrong? I have write my function in my templates function.php file.
I just created a page template called restriction_page.php:
<?php
/*
Template Name: My Restricted Page
*/
?>
<h1>Hello There</h1>
<?php
function restrict_access_if_logged_out(){
if (!is_user_logged_in() && !is_home()){
wp_safe_redirect(wp_login_url());
}
}
add_action( 'wp', 'restrict_access_if_logged_out', 3 );
?>
i set this template as the fefault template in my about page. No solution.
Any ideas?