如何通过用户ID实现wordpress登录用户

时间:2016-10-31 13:05:37

标签: php wordpress

我一直试图用用户ID来实现wordpress登录。

这是我的代码,但它不起作用。代码不会记录用户。

// check if user has an account...
$details = get_user_by("email", $user->emailAddress);
//check user if not empty...
if ($details != null) {      
  // create a cookie and log user in...
  wp_set_current_user($details->ID, $details->user_login);
  wp_set_auth_cookie($details->ID);
  do_action('wp_login', $details->ID);

  //redirect to homepage...
  header("Location: " . site_url());
}

1 个答案:

答案 0 :(得分:1)

文档说明函数get_user_by()在失败时返回false,如果成功则返回用户对象,但在代码中你检查NULL,所以也许你可以试试这个:

if (is_object($details)) {      
    //your code
}

or 

if ($details != false) {
    //your code
}