Wordpress force user to login before view any site

2019-01-23 08:06发布

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..

3条回答
2楼-- · 2019-01-23 08:16

I think you are missing a parenthesis:

if ( ( is_single() || is_front_page() || is_page() ) 
       && !is_page('login') && !is_user_logged_in()){ 
    auth_redirect(); 
} 
查看更多
相关推荐>>
3楼-- · 2019-01-23 08:29

Easy way

// Require login for site
get_currentuserinfo();
global $user_ID;
if ($user_ID == '') { 
  header('Location: /wp-login.php'); exit(); 
} 
查看更多
Juvenile、少年°
4楼-- · 2019-01-23 08:36

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

查看更多
登录 后发表回答