There is often the question of using an email for a username. In WordPress, there is never a good reason to do this because the core WP schema does not allow for it. This post covers why you should work within the existing schema, how you can do that, and provides simple code snippets you can use to customize the WP-Members login and registration forms to handle it.
There are three fields required to create a user, two of which must be unique:
- user_login (unique)
- user_email (unique)
- user_pass (a password)
Without all three of these, you cannot create a new user using WP’s functions. There may be third-party applications (such as plugins) that work around this, but anything that is not done within this schema is asking for trouble at some point.
What about simply inserting an email into the user_login field?
Not a good idea. WP does not allow for this field to be changed. Sure, you can do it directly in the DB with an update query, but what if you have a third party app that causes a problem with that? Also, what about privacy issues with displaying emails. When you display the username in another application like a forum, you’ll be exposing the user’s email address – sometimes to their disdain.
No, it’s best to work within the schema provided and not try to bend it to your will in ways that it was not intended.
So can you remove the username field in WP-Members?
Yes, you can. You will need to filter it out of the form. AND, you will need to insert a filler value into the field before the user is inserted because this field is required to create a user. But it’s not difficult, and since it’s not difficult, then you are better off doing that instead of working outside the scope of the application.
The following code snippet will handle it:
/** * Removes the username field from the * WP-Members registration form using * the `wpmem_fields` filter. */ add_filter( 'wpmem_fields', function( $fields, $tag ) { if ( 'register' == $tag ) { unset( $fields['username'] ); } return $fields; }, 10, 2 ); /** * Inserts a filler value for the username * field using the user's email address so * that the user can be inserted. * * Uses the `wpmem_pre_validate_form` filter * and `wpmem_create_username_from_email() */ add_filter( 'wpmem_pre_validate_form', function( $post_data, $tag ) { if ( 'register' == $tag ) { $post_data['username'] = wpmem_create_username_from_email( $post_data['user_email'] ); } return $post_data; }, 10, 2 ); /** * This filters the text for the main body * login form and the widget to only display * "Email" instead of "Username or email". */ add_filter( 'wpmem_default_text', function( $text ) { $text['login_username'] = 'Email'; $text['widget_login_username'] = 'Email'; return $text; });
What’s happening in these code snippets?
The first one is a filter for the registration form, which is used for registration, but also for user profile update. If it’s the new registration process, the filter removes the username field.
The second filter uses the wpmem_pre_validate_form
filter so that prior to the form fields being validated to make sure everything is OK before insertion, it puts a value into the username field. Noting that we want this to be seamless for the user, and we need a unique value, it uses wpmem_create_username_from_email()
to generate a value for us. This is a function in the WP-Members API that will generate a value from the user’s email address (everything before the “@” symbol), check to make sure that is a unique value (that it doesn’t already exist in the db), and it will rerun until the generated value is unique. It’s important to point out, the user doesn’t need to see this value nor know about it. They don’t need it to log in. They can log in with their email address.
The last filter removes mention of username from the login form so that it just says “Email” for the input field.