2016-12-16 63 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