Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm trying to fork this plugin to use the user email to login, as opposed to Username.

The plugin is http://wordpress.org/plugins/wp-modal-login/

Is there a nice way to do this, perhaps by a filter to get this plugin to work with email credentials?

I've tried using this filter:

function custom_login() {
  $data = array();        
  $data['user_login']     = sanitize_user( $_REQUEST['username'] );
  $data['user_email']     = sanitize_user( $_REQUEST['user_email'] );
  $data['user_password']  = sanitize_text_field( $_REQUEST['password'] );
  $data['rememberme']     = sanitize_text_field( $_REQUEST['rememberme'] );
  $data['user_login']     = sanitize_user( $_REQUEST['user_email'] );
  $user = wp_signon( $creds, false );
  if ( is_wp_error($user) )
    echo $user->get_error_message();
}
// run it before the headers and cookies are sent
add_action( 'after_setup_theme', 'custom_login' );
share|improve this question
1  
Do you want to FORCE email-only log in? or just allow email log in ALSO? –  Jake Aug 28 '13 at 13:33

2 Answers 2

up vote 4 down vote accepted
+25

I'm using this simple action, and it works like a charm. :)

<?php
/** Plugin Name: (#111223) User Login with Mail Address */
    add_action( 'wp_authenticate', 'wpse111223_login_with_email_address' );
function wpse111223_login_with_email_address( $username ) {
    $user = get_user_by_email( $username );
    if ( ! empty( $user->user_login ) )
        $username = $user->user_login;

    return $username;
}

It hooks to wp_authenticate and enables email login to the WordPress Authentication system, so it should work with any plugin. I use it successfully with Login with Ajax plugin.

:)

Later-edit: For those who don't know where to put the snippet of code, it should go in your theme's functions.php or a custom plugin.

share|improve this answer

I use this basic method to enable email log in, similar to another answer, but I think it is cleaner to test first:

add_filter( 'authenticate', 'custom_allow_email_login', 20, 3);
function custom_allow_email_login( $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 );
}

Unfortunately, if you want to change the label on the form, you have to either use Javascript or create your own log in page, with its own, customizable form.

Using Javascript:

add_action( 'login_enqueue_scripts', 'custom_login_enqueue_scripts' );
function custom_login_enqueue_scripts()
{
    wp_enqueue_script('jquery');
}
add_action( 'login_form', 'custom_change_username' );
function custom_change_username()
{
    echo "\n" . '
    <script type="text/javascript">
        jQuery(document).ready(function($) {
            $("label").html(function(index,html){
                return html.replace("Username", "Username or Email");
            });
        });
    </script>' . "\n";
}

And you can use wp_login_form to include a log in form on your own page.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.