Update:我已经创建了一个插件,用于登录、注册和通过电子邮件检索密码。https://wordpress.org/plugins/smart-wp-login/
简而言之,您可以将WordPress配置为通过电子邮件登录。
三个步骤:
删除默认身份验证功能添加自定义身份验证功能更改wp登录中的文本“用户名”。php至“电子邮件”注意:
不要编辑核心文件移除WordPress默认身份验证功能
WordPress使用“authenticate“筛选以在用户登录时执行其他验证。
remove_filter(\'authenticate\', \'wp_authenticate_username_password\', 20);
添加自定义身份验证功能
add_filter(\'authenticate\', function($user, $email, $password){
//Check for empty fields
if(empty($email) || empty ($password)){
//create new error object and add errors to it.
$error = new WP_Error();
if(empty($email)){ //No email
$error->add(\'empty_username\', __(\'<strong>ERROR</strong>: Email field is empty.\'));
}
else if(!filter_var($email, FILTER_VALIDATE_EMAIL)){ //Invalid Email
$error->add(\'invalid_username\', __(\'<strong>ERROR</strong>: Email is invalid.\'));
}
if(empty($password)){ //No password
$error->add(\'empty_password\', __(\'<strong>ERROR</strong>: Password field is empty.\'));
}
return $error;
}
//Check if user exists in WordPress database
$user = get_user_by(\'email\', $email);
//bad email
if(!$user){
$error = new WP_Error();
$error->add(\'invalid\', __(\'<strong>ERROR</strong>: Either the email or password you entered is invalid.\'));
return $error;
}
else{ //check password
if(!wp_check_password($password, $user->user_pass, $user->ID)){ //bad password
$error = new WP_Error();
$error->add(\'invalid\', __(\'<strong>ERROR</strong>: Either the email or password you entered is invalid.\'));
return $error;
}else{
return $user; //passed
}
}
}, 20, 3);
在wp登录中更改文本“用户名”。php至“电子邮件”
我们可以使用gettext 筛选以将文本“用户名”更改为“电子邮件”,而不编辑核心文件。
add_filter(\'gettext\', function($text){
if(in_array($GLOBALS[\'pagenow\'], array(\'wp-login.php\'))){
if(\'Username\' == $text){
return \'Email\';
}
}
return $text;
}, 20);
我还在我的博客上写了一篇详细的文章
http://www.thebinary.in/blog/wordpress-login-using-email/