How to Allow Users to Set Password During Registration
After learning how to register additional contact information fields and display them in WordPress register form, it’s time to take a look at how to allow users to set their own passwords when they register. You can achieve that using the same logic from previous post, with slightly different custom functions that you’ll hook into some WordPress filter and action hooks.
Once again, we begin by showing a few extra fields in WordPress registration form and checking user’s input. This time, it’s password and repeat password field, but in order to fight spam account we’ll add one of those beloved “Are you human?” fields as well. Here’s the code:
Our ts_check_extra_register_fields function performs three checks:
- First it makes sure passwords match
- Then it checks if password is at least eight characters long
- Finally, it compares value from “are you human” field to WordPress site title
If any of these conditions are not met, error message will be displayed above the register form. If all is well, WordPress will move on to creating a new user account. But, since we want WordPress to store password into database, rather than generate its own, we need to hook our ts_register_extra_fields function into user_register action hook:
It’s dead simple, if password field was left empty our function does nothing. If the field has a certain value, it passes it to $userdata array as $userdata['user_pass'].
That would be all if it wasn’t for that annoying “A password will be e-mailed to you.” message near the end of WordPress registration form. Luckily, it can be easily replaced by hooking into gettext filter hook:
Why would you want to do this? Well, while most users won’t mind having a password set for them, it still might be better to treat them like humans and let them set their own. Of course, you can copy all the code to functions.php file inside your theme folder or turn it into a plugin by simply merging it into one PHP file and adding standard plugin information (plugin header) to it.