Home / functions.php / Login with username or email address
  • How to Login with username or email address

    login-with-username-or-email-address

    Adding this snippet to the functions.php of your wordpress theme will let users login using an email address or a username. The second snippet will change the text on the login page from “username” to “username / email” but feel free to change the login text to anything you would like.

    Try out Capture users last login date and time snippet for even more login awesomeness.

    ( click code to copy )

    wordpress snippet : PHP

    <>
    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');
    function change_username_wps_text($text){
           if(in_array($GLOBALS['pagenow'], array('wp-login.php'))){
             if ($text == 'Username'){$text = 'Username / Email';}
                }
                    return $text;
             }
    add_filter( 'gettext', 'change_username_wps_text' );
    

    ( WordPress codex functions, hooks, in this snippet. )

    authenticate, gettext, get_user_by, wp_authenticate, add_filter, add_action, _e, wp,
  • Sola

    cool tip,works like a charm

  • Sahil

    Hey,. thanks for the snippet. Is their any snippet to disable viewing
    post drafts in “All Post” section. I mean Guest authors can only view
    their own post drafts, but not of others. Is it possible??

  • So cool :D.

  • Chandresh

    Works totally

  • Debbie Campbell

    Perfect!

  • pietergoosen

    hi Kevin, it works great on the wordpress default login page. Any ideas on the code for a custom login widget. No need for the second code, i will manually change this in my widget. Many Thanks

  • pietergoosen

    OK, got it working. Just one little mistake on my side

  • JOnas

    Muito BOm , me ajudou bastante!

  • Pingback: 25 WordPress Code Snippets for Enhanced Performance - ManageWP()

  • Pragam

    Working good………………….
    Great…………….:)