只允许特定用户角色登录WordPress网站

问题描述:

我目前有一个WP网站设置,其中包含一个专门为安装WooCommerce产品的单个管理区域而安装的独特“多站点”插件,但有2个不同的前端基于2不同的领域,具有不同的主题。只允许特定用户角色登录WordPress网站

其中一个网站是“批发”网站,而另一个是“零售”网站。批发网站应该只允许交易客户进行购买。问题在于两个网站共享一个域名,因此共享用户帐户。

问题:我需要确保没有用户角色'trade_customer'的用户是否尝试登录到批发站点,检查角色并将用户注销,重定向到登录页面通知。到目前为止,我已经中的functions.php如下:

function trade_customers_only() { 
    function get_user_role() { 
    global $current_user; 

    $user_roles = $current_user->roles; 
    $user_role = array_shift($user_roles); 

    return $user_role; 
} 

$the_current_role = get_user_role(); 
echo $the_current_role; 
    if($the_current_role != 'administrator') { 
     $logout_url = wp_login_url().'?mode=tradeonly'; 
     wp_destroy_current_session(); 
     wp_logout(); 
     wp_redirect($logout_url, 302); 
     exit(); 
    }  
} 
add_action('wp_login', 'trade_customers_only'); 

// CUSTOM LOGIN MESSAGES 
function my_login_message() { 

    if($_GET['mode'] == 'tradeonly'){ 
     $message = '<p class="message"><b>You must be a Trade Customer to access this site.</b></p>'; 
     return $message; 
    } 

} 
add_filter('login_message', 'my_login_message'); 

该代码目前:返回登录的用户可WP-login.php中并添加注释“你必须是一个行业客户...等等” 。但是,在任何用户角色的第一次登录尝试之后,每个其他登录尝试都会执行相同的重定向并显示消息。我的代码是不正确的还是在数据库或浏览器中存在一些WP会话cookie导致WP认为我没有使用管理员帐户的问题?

我第一次尝试登录时使用管理员帐户。它的工作和去仪表板。下一次尝试是使用客户角色帐户。发生了重定向和注释。以管理员帐户进行以下尝试仅使用备注进行重定向,但没有仪表板访问权限。

1)更改trade_customers_only功能:

function trade_customers_only($login, $user) { 
    if($user->roles && !in_array('administrator',$user->roles)) { 
     $logout_url = wp_login_url().'?mode=tradeonly'; 
     wp_destroy_current_session(); 
     wp_logout(); 
     wp_redirect($logout_url, 302); 
     exit(); 
    } 
} 

和修复你的行动呼吁:另一种解决方案是使用认证过滤器,而不是使用wp_login

add_action('wp_login', 'trade_customers_only',10,2);  

2)行动。区别在于您在用户会话设置之前检查用户的角色,因此您不需要销毁它。

add_filter('authenticate',function($user,$username) { 
    if (!is_wp_error($user)) { 
     $auth_user=get_user_by('login',$username); 
     if ($auth_user && !in_array('administrator',$auth_user->roles)) { 
      return new WP_Error('authentication_failed', '<p class="message"><b>You must be a Trade Customer to access Key Essentials. Are you looking for <a href="https://lovetillys.co.uk" title="Love Tillys">Love Tillys?</a></b></p>'); 
     } 
    } 
    return $user; 
},100,2); 

我的新的全代码现在:

function trade_customers_only($login, $user) { 
    if($user->roles && !in_array('administrator',$user->roles)) { 
     $logout_url = wp_login_url().'?mode=tradeonly'; 
     wp_destroy_current_session(); 
     wp_logout(); 
     wp_redirect($logout_url, 302); 
     exit(); 
    } 
} 
add_action('wp_login', 'trade_customers_only',10,2); 

// CUSTOM LOGIN MESSAGES 
function my_login_message() { 

    if($_GET['mode'] == 'tradeonly'){ 
     $message = '<p class="message"><b>You must be a Trade Customer to access this site.</b></p>'; 
     return $message; 
    } 

} 
add_filter('login_message', 'my_login_message'); 

此代码工作正常。然而正如Kulivov谢尔盖在回复中提到的那样,使用验证码筛选器而不是wp_login 动作更适合我需要实现的功能。使用:

add_filter('authenticate',function($user,$username) { 
    if (!is_wp_error($user)) { 
     $auth_user=get_user_by('login',$username); 
     if ($auth_user && !in_array('administrator',$auth_user->roles)) { 
      return new WP_Error('authentication_failed', '<p class="message"><b>You must be a Trade Customer to access this site.</b></p>'); 
     } 
    } 
    return $user; 
},100,2); 

用户角色不仅检查不登录并创建一个会话,它也一直没有重定向,这是伟大的当前页面上的用户。