Wordpress force user to login before view any site

2019-01-23 07:29发布

问题:

In wordpress I am trying to force user login first before see anything. Means I want my user to first login to view any single page, post or archive and only they can se FAQ page. I am trying with below code but problem is as soon as I loged in, its redirect me again to login page.

However my site structure would be like below and in same manner I want this code to work...

// Force user to login on welcome
function my_force_login() {
global $post;

    if (is_single() || is_front_page() || is_page() && !is_page('login') && !is_user_logged_in()){ 
        auth_redirect(); 
    }   
}

Welcomd-Login screen (like facebook) -Home page -Blog -About -contact -FAQ

So I want to allow visitor only can see Welcome Login screen and FAQ, contact page to view rest of the site I want to force them to login..

I need great help of you people.. Thanks a lot in advance..

回答1:

I think you are missing a parenthesis:

if ( ( is_single() || is_front_page() || is_page() ) 
       && !is_page('login') && !is_user_logged_in()){ 
    auth_redirect(); 
} 


回答2:

There's a plugin you can use for this. Private Only



回答3:

Easy way

// Require login for site
get_currentuserinfo();
global $user_ID;
if ($user_ID == '') { 
  header('Location: /wp-login.php'); exit(); 
}