首页 > Wordpress > WordPress 实现用户使用Email地址登录
2015
04-27

WordPress 实现用户使用Email地址登录

WordPress 实现用户使用Email地址登录 - 第1张  | 最后的面包
对于忘记注册的用户名这种情况,可谓再常见不过了……幸运的是,您可以在登录界面增加一个“使用Email登录”的选项,这样,我们可爱的用户应该不会再为登录不上 WordPress 而苦恼了吧?

代码添加 Email 登录功能,添加以下代码至 Functions.php:

  1. //remove wordpress authentication  
  2. remove_filter(‘authenticate’, ‘wp_authenticate_username_password’, 20);  
  3.    
  4. add_filter(‘authenticate’, function($user, $email, $password){  
  5.    
  6.     //Check for empty fields  
  7.         if(empty($email) || empty ($password)){          
  8.             //create new error object and add errors to it.  
  9.             $error = new WP_Error();  
  10.    
  11.             if(empty($email)){ //No email  
  12.                 $error->add(’empty_username’, __(‘<strong>ERROR</strong>: Email field is empty.’));  
  13.             }  
  14.             else if(!filter_var($email, FILTER_VALIDATE_EMAIL)){ //Invalid Email  
  15.                 $error->add(‘invalid_username’, __(‘<strong>ERROR</strong>: Email is invalid.’));  
  16.             }  
  17.    
  18.             if(empty($password)){ //No password  
  19.                 $error->add(’empty_password’, __(‘<strong>ERROR</strong>: Password field is empty.’));  
  20.             }  
  21.    
  22.             return $error;  
  23.         }  
  24.    
  25.         //Check if user exists in WordPress database  
  26.         $user = get_user_by(’email’, $email);  
  27.    
  28.         //bad email  
  29.         if(!$user){  
  30.             $error = new WP_Error();  
  31.             $error->add(‘invalid’, __(‘<strong>ERROR</strong>: Either the email or password you entered is invalid.’));  
  32.             return $error;  
  33.         }  
  34.         else//check password  
  35.             if(!wp_check_password($password, $user->user_pass, $user->ID)){ //bad password  
  36.                 $error = new WP_Error();  
  37.                 $error->add(‘invalid’, __(‘<strong>ERROR</strong>: Either the email or password you entered is invalid.’));  
  38.                 return $error;  
  39.             }else{  
  40.                 return $user; //passed  
  41.             }  
  42.         }  
  43. }, 203);  
最后编辑:
作者:漱石
这个作者貌似有点懒,什么都没有留下。
捐 赠如果您觉得这篇文章有用处,请支持作者!鼓励作者写出更好更多的文章!

WordPress 实现用户使用Email地址登录》有 1 条评论

  1. Pingback 引用通告: WordPress 实现用户使用Email地址登录-MTPAN资源福利分享

留下一个回复