對於忘記註冊的用户名這種情況,可謂再常見不過了……幸運的是,您可以在登錄界面增加一個 「使用 Email 登錄」 的選項,這樣,我們可愛的用户應該不會再為登錄不上 WordPress 而苦惱了吧?

代碼添加 Email 登錄功能,添加以下代碼至 Functions.php:

  1. //remove WordPress authentication  
  2. 20);  
  3.    
  4.    
  5. //Check for empty fields  
  6.         if(empty($email) || empty ($password)){          
  7. //create new error object and add errors to it.  
  8.             $error = new WP_Error();  
  9.             if(empty($email)){ //No email  
  10.             }  
  11. else if(!filter_var($email, FILTER_VALIDATE_EMAIL)){ //Invalid Email  
  12.                 $error->add(『invalid_username』, __(『<strong>ERROR</strong>: Email is invalid.』));  
  13.    
  14. if(empty($password)){ //No password  
  15.                 $error->add(』empty_password』, __(『<strong>ERROR</strong>: Password field is empty.』));  
  16.    
  17. return $error;  
  18.         }  
  19.         //Check if user exists in WordPress database  
  20.    
  21. //bad email  
  22.         if(!$user){  
  23. new WP_Error();  
  24.             $error->add(『invalid』, __(『<strong>ERROR</strong>: Either the email or password you entered is invalid.』));  
  25. return $error;  
  26.         }  
  27. else//check password  
  28.             if(!wp_check_password($password, $user->user_pass, $user->ID)){ //bad password  
  29. new WP_Error();  
  30.                 $error->add(『invalid』, __(『<strong>ERROR</strong>: Either the email or password you entered is invalid.』));  
  31. return $error;  
  32.             }else{  
  33. return $user; //passed  
  34.             }  
  35. }, 203);