0

I would like to let the user login either using username or email.

The only way to find the email is by detecting @ symbol using strpos.

However WordPress allow @ symbol in username too.

Can someone help me to invalidate username if it contains @ symbol in it..

1 Answer 1

2

In a multi-site installation it is 'wpmu_validate_user_signup':

add_filter( 'wpmu_validate_user_signup', 'wpse_77904_mu_no_at_in_username' );

function wpse_77904_mu_no_at_in_username( $user )
{
    if ( FALSE !== strpos( $user['user_name'], '@' ) )
        $user['errors']->add('user_name',  __( 'That username is not allowed.' ) );

    return $user;
}

On single-site it is 'validate_username':

add_filter( 'validate_username', 'wpse_77904_no_at_in_username', 10, 2 );

function wpse_77904_no_at_in_username( $valid, $username )
{
    if ( FALSE !== strpos( $username, '@' ) )
        return FALSE;

    return $valid;
}

Caveat: both not tested. :)

1
  • Great.. It works :) Commented Jan 1, 2013 at 19:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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