2017-05-04 98 views
0

我目前有一個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

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); 
0

我的新的全代碼現在:

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); 

用戶角色不僅檢查不登錄並創建一個會話,它也一直沒有重定向,這是偉大的當前頁面上的用戶。

相關問題