Skip to content

Instantly share code, notes, and snippets.

@norcross
Last active August 6, 2017 22:52
Show Gist options
  • Save norcross/ba1dd4e89223b10c1f2d to your computer and use it in GitHub Desktop.
Save norcross/ba1dd4e89223b10c1f2d to your computer and use it in GitHub Desktop.
allow email address to be used as login name
<?php
add_filter ( 'authenticate', 'rkv_login_options', 20, 3 );
function rkv_login_options( $user, $username, $password ) {
if ( is_email( $username ) ) {
$user = get_user_by( 'email', $username );
}
if ( $user ) {
$username = $user->user_login;
}
return wp_authenticate_username_password( null, $username, $password );
}
@Jeradin
Copy link

Jeradin commented Jun 6, 2014

Worth removing the authenticate filter first or else you get an PHP error: "Notice: Undefined property: WP_Error::$user_login....."

remove_filter( 'authenticate', 'wp_authenticate_username_password', 20, 3 );

Also for the Label and error message you can add these filters.

function anagram_username_or_email_login_new( $translated, $original, $domain ) {
   if(in_array( $GLOBALS['pagenow'], array( 'wp-login.php', 'wp-register.php' ) ) ){
        if(  $translated == 'Username' ) {
             $translated = __( 'Username or Email', 'anagram' );
        }
    }
   return $translated;
}
add_filter( 'gettext', 'anagram_username_or_email_login_new', 10, 3 );

add_filter( 'wp_login_errors', 'anagram_incorrect_username_message', 10, 2 );
function anagram_incorrect_username_message( $errors, $redirect_to ) {
    if( isset( $errors->errors['invalid_username'] ) ) {
        $errors->errors['invalid_username'][0] = sprintf( __( '<strong>ERROR</strong>: Invalid Username or Email. <a href="%s" title="Password Lost and Found">Lost your password</a>?' ), wp_lostpassword_url() );
    }
    if( isset( $errors->errors['empty_username'] ) ) {
        $errors->errors['empty_username'][0] = '<strong>ERROR</strong>: The username/Email field is empty.';
    }

    return $errors;
 }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment