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..

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. :)

| improve this answer | |
  • Great.. It works :) – Giri Jan 1 '13 at 19:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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