2016-12-16 103 views
1

只希望訂閱者的角色用戶登錄到應用程序。我不知道獲得在WordPress用戶的角色。這裏是我的代碼在wordpress中以用戶的用戶角色登錄

$u_name = $_POST['userName']; 
$password = $_POST['password']; 
$username = $u_name; 

//$tokenID = $_GET['tokenID']; 
$vendor_list = array(); 
if (username_exists($username)) 
    { 

     $user = get_user_by('login', $username); 

     if ($user && wp_check_password($password, $user->user_pass, $user->ID)) 
     { 
     $email = $user->user_email; 
     $vendor_list[] = array("userID"=>$user->ID,"userName" =>$username,"email" =>$email); 
     echo json_encode($vendor_list); 
     } 
     else 
     { 
     $testing = 2; 
     echo json_encode($testing); 
     } 
    } 
    else 
    { 
     $testing = 1; 
     echo json_encode($testing); 
    } 

回答

1
$u_name = $_POST['userName']; 
$password = $_POST['password']; 
$username = $u_name; 

//$tokenID = $_GET['tokenID']; 
$vendor_list = array(); 
if (username_exists($username)) 
    { 

     $user = get_user_by('login', $username); 

     if ($user && wp_check_password($password, $user->user_pass, $user->ID) &&) 
     { 
     $users = wp_get_current_user(); 
      if(in_array('subscriber', (array) $users->roles)) 
      { 
       $email = $user->user_email; 
       $vendor_list[] = array("userID"=>$users->ID,"userName" =>$username,"email" =>$email); 
       echo json_encode($vendor_list); 
      } 
      else 
      { 
       $testing = 3; 
       echo json_encode($testing); 
      } 
     } 
     else 
     { 
     $testing = 2; 
     echo json_encode($testing); 
     } 
    } 
    else 
    { 
     $testing = 1; 
     echo json_encode($testing); 
    } 
+0

灰您好,感謝您的答覆,但你可以請讓我知道我如何在我的代碼使用。我在哪裏使用這種情況?查詢後: – Anuradha

+0

:$ user = get_user_by('login',$ username);您可以使用此代碼,並根據代碼中的用戶角色應用條件。 –

+0

你好阿什,這不工作顯示錯誤。 – Anuradha