In this post, we will cover a number of ways that you can control the allowed format and structure of usernames for new members.
Allow members to use an email address as their username.
If your site doesn’t have a need for public or frontend-facing usernames, you may want to simplify checkout by setting a user’s email address as their username. With the vast number of sites we are all members of, it has become extremely difficult to remember what username I created where. It’s much easier to remember your login credentials when using an email address as your login name.
During a default membership checkout, users are prompted to create a “Username” and enter their “Email Address”. This code recipe will take the information inserted in the Email Address field of the Membership Checkout page and use it as the member’s username.
To prevent any confusion, you will also want to hide the “Username” field using CSS (more on this below) or by using a Custom Page Template. If you do not hide this field and a member performs a checkout and inputs a username then the Code Recipe will still overwrite the username data and replace it with the member’s email address.
The Code Recipe
Reformat usernames to use allowed characters after checkout
This code recipe will convert a member’s username to match your specific rules. In this example, we convert all characters to lowercase and replace any spaces found in the username with underscores. If a member inputs a username like “John Doe”, then this will be changed to “john_doe”.
One of our members recently had a support request related to the format of usernames. Their request was for appearance reasons, but there are other cases where you would like usernames to follow stricter guidelines. For example, if you are using a single sign on bridge to integrate your members with a third-party application, you may need to match the requirements of usernames for that external site.
This code can also be useful in cases where the member’s username appears in other features of your site, such as on a BuddyPress profile.