How to register WordPress users with email only
Today we look into how to simplify the WordPress user registration process. We go through a solution to allow WordPress registration with email only and replace or remove the need for the username field.
When it comes to user registration and interactions, usually less is more. Thus, a registration form with fewer fields tends to be better for your users. These simpler forms have a much better conversion rates, and usually, the username is quick field to get rid of. That’s because this field has little use to we as admins and to our visitors.
A nice thing to keep in mind is that WordPress already supports emails as a replacement for usernames. In fact, you can use an WordPress email as username. In addition, it’s possible to use the email in your WordPress login forms, and it will work just as well.
The caveat here is that displaying your users’ email address is not a good idea. Hence, if you have comments sections or other sections with user interaction you need something else.
Therefore, our solution for today is to add a bit of custom code in your WordPress website. This code allows WordPress to register email as your username, or a modified version of it. We go over the different steps you need to make this change possible, from the frontend to the backend and the best part is that you won’t have to use dedicated WordPress plugin.
Additionally, this code can be used for further customization ideas. As this affects the WordPress user registration and login forms.
Let’s get started!
Analyze the WordPress register, email and username merging
Just in case you’ve forgotten, this is how our WordPress register page looks like:
The first step is just visual. We need to remove the WordPress username field so new users won’t see it. The thing is, removing that element is much harder than simply hiding it. Let’s hide it with a bit of CSS then. In order to do this, we’ll use the WordPress login_head
hook, so we can print some custom CSS for the registration page:
add_action('login_head', function(){ ?> <style> #registerform label[for="user_login"], #registerform input#user_login { display:none; } </style> <?php });
This is usually how the WordPress registration (and login) for is customized, using the login_head
hook. The interesting part is that these functions can be used to add new fields if you want. For instance, you can use a bit of jQuery and create new form fields there.
Then, we get this result after our code change:
It seems like it is ready, right? Well, we still need to change how the code works after the data submission.
How to create a username for your users
Even though we don’t have a username field in our form anymore, WordPress still requires it. That’s because internally the WordPress unique username is important for WordPress’ functions.
We have a few options on how to handle this. Here is what we came up with:
- Assign a random username
- Simply copy the email and use it as the username
- Just copy the email handle
These different options work well with different types of sites.
If you need user privacy above everything the first option is good for you. With it, you can still trace back which user did what, but visitors won’t know who that user is.
The second option works only if you have no public display for usernames. For instance, if you have a comments section, this approach can be problematic as the user’s email can get exposed.
The third option works well for an in-between option. Users are still recognizable in your WordPress site, but they won’t risk being spammed. With this approach however you would also need to implement an additional check to ensure the uniqueness of the username.
To keep the example simple, we’ll use option 2 where we apply the email address as a WordPress username:
add_action('login_form_register', 'ui_set_registration_username'); function ui_set_registration_username(){ //if there is anything set for user email if( isset($_POST['user_email']) && ! empty( $_POST['user_email'] ) ){ //replace login with user email $_POST['user_login'] = $_POST['user_email']; } }
Then we need to remove the error handling, so that we don’t get the “Empty username” error message when submitting the form.
//Remove error for username, only show error for email only. add_filter('registration_errors', 'ui_registration_errors', 10, 3); function ui_registration_errors($wp_error, $sanitized_user_login, $user_email){ if(isset($wp_error->errors['empty_username'])){ unset($wp_error->errors['empty_username']); } return $wp_error; }
And finally, we’ll adjust the login page label. At the moment, it says “Username or Email Address” but we need it to show just “Email Address”. This is done here:
//replace WP strings with our own custom strings add_filter('gettext', 'ui_custom_string', 20, 3); function ui_custom_string( $translated_text, $text, $domain ) { if($translated_text == 'Username or Email Address'){ //you can add any string you want here, as a case return 'E-mail Address'; } return $translated_text; }
And this is the final result for our login page:
How to process your user registrations
Once our code changes are ready, we need to check their effectiveness. A good first step would be sending a message to your past users to let them know about this change. After all, they will see a simplified login form now. You can filter them based on their registration date:
You can export these users, then contact them using MailChimp or similar.
Since our main goal is making our users’ lives easier, it would be nice to measure this. A good metric to use is the number of registrations we have after the big change.
Additionally, you can get an overview of your user registrations in the reports section. You can check this under Users Insights > Reports:
Then, we can check user activity past our change. For instance, are our recent users more active? Well, we can check that by filtering them, then filter the most active users.
Conclusion
Today we looked into how to make your WordPress registration better with email only. Thus, we removed the username field and replaced it with a function. In addition, we saw different options to replace the username, based on privacy and WordPress security concerns. Next, we looked into how to analyze the results of this change. Therefore, we dived into different metrics regarding user activity.
We hope you enjoyed this article and see you again next time!