admin管理员组文章数量:1426839
For the login, I want to enable the user to use the email instead of the username. This is what I got so far:
remove_filter('authenticate', 'wp_authenticate_username_password', 20);
function login_with_email_address($username) {
$user = get_user_by_email($username);
if(!empty($user->user_login))
$username = $user->user_login;
return $username;
}
add_action('wp_authenticate','login_with_email_address');
The result of this, is the following error message:
"Invalid username or password. Please try again!"
For the login, I want to enable the user to use the email instead of the username. This is what I got so far:
remove_filter('authenticate', 'wp_authenticate_username_password', 20);
function login_with_email_address($username) {
$user = get_user_by_email($username);
if(!empty($user->user_login))
$username = $user->user_login;
return $username;
}
add_action('wp_authenticate','login_with_email_address');
The result of this, is the following error message:
Share Improve this question edited Oct 13, 2015 at 13:12 kaiser 50.9k27 gold badges151 silver badges245 bronze badges asked Oct 6, 2015 at 6:36 user3811650user3811650 157 bronze badges 9 | Show 4 more comments"Invalid username or password. Please try again!"
1 Answer
Reset to default 1Your code has some problems:
- You are using deprecated function
get_user_by_email
- You are using wrong hook
wp_authenticate
You can this plugin https://wordpress/plugins/wp-email-login/
or
Your can use this code
function wpsc_authenticate_user_by_email( $user, $username, $password ) {
// Bailout
if( ! is_frontend_login_form() ){
return $user;
}
$login_page = strtok( $_SERVER["HTTP_REFERER"], '?' );
//check for valid inputs
if( $username == "" || $password == "" ) {
wp_redirect( $login_page . "?login=empty" );
exit;
}
// get user by email
if ( is_email( $username ) ) {
$user = get_user_by( 'email', $username );
// validate user
if ( $user && wp_check_password( $password, $user->user_pass, $user->ID ) ) {
return $user;
}else{
wp_redirect( $login_page . '?login=failed' );
exit;
}
}else{
wp_redirect( $login_page . '?login=failed' );
exit;
}
return $user;
}
add_filter( 'authenticate', 'wpsc_authenticate_user_by_email', 1, 3 );
Define conditions in is_frontend_login_form()
to check if user is coming from your frontend form or not.
本文标签: authenticationI want login using email not username wordpress front end
版权声明:本文标题:authentication - I want login using email not username wordpress front end 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745478650a2660078.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
functions.php
file but then what is the problem? What is your question? Are you getting any error? Explain your issue. – Robert hue Commented Oct 6, 2015 at 7:06remove_filter('authenticate', 'wp_authenticate_username_password', 20); function login_with_email_address($username) { $user = get_user_by_email($username); if(!empty($user->user_login)) $username = $user->user_login; return $username; } add_action('wp_authenticate','login_with_email_address');
– user3811650 Commented Oct 6, 2015 at 7:15