How do I add my own custom fields to the WordPress registration form ?

Introduction :

In this article, you will learn how to create custom fields for registration forms. If you want to allow registrations on WordPress sites, you need to collect detailed information about the users. If you really need this information, we recommend that you make it mandatory when registering. Otherwise, the user may actually access the profile page and provide additional information. Of course, there are many free and excellent add-ons. It has already been completed, but you can do it manually. This is because the plugin can not execute exactly what you need, or because you want to make the site as light as possible.

Pic : Allow registrations on WordPress site
How do I add my own custom fields to the WordPress registration form ?

The standard WordPress registration form consists of two form fields: user name and e-mail. Having only a user name and email field makes registration process very easy, lets prepare a registration form directly with some extra form fields in addition to e-mail such as user name and mobile phone number.

Pic : Standard WordPress registration form
How do I add my own custom fields to the WordPress registration form ?

In this article, we’ll add a required field that accepts the user’s mobile phone number will be visible in

1. WordPress Frontend Registration page
2. Manually Add a WordPress User with Administrator
3. WordPress Press user profile page

The main hooks used to add a custom field to the WordPress above sections are

a). register_form (to add new HTML form elements for frontend registration page.)

b). registration_errors (to complete validation in the frontend registration form fields.)

c). user_register (to save a frontend custom field value, if there is no error.)

d). show_user_profile (this action hook invoked whenever a user modifies another user’s profile, using this add new HTML form elements to user profile )

e). edit_user_profile (this action hook is invoked each time the user edits his or her user profile, using this to add new HTML form elements to user profile )

f). personal_options_update (this action only takes place if the current user edits their profile, using this to update user profile custom field value, if there is no error)

g). edit_user_profile_update ( this action only takes place if the current user edits another user’s profile, using this to update user profile custom field value, if there is no error)

h). user_new_form (this action hook is invoked at the end of the new user form, using this to add new HTML form elements to create user at administrator registration page)

i). user_profile_update_errors (to complete validation in the administrator registration form fields.)

k). edit_user_created_user (this action hook is invoked after creating a new user, using this to add user custom field value, if there is no error)

Now let’s prepare method for each section to add example extra form field mobile phone number.

1.You must create a variable that contains custom fields.
The default page of registration also stores all the fields in the array with three parameters. The first field is the field ID, the second is the field identifier, and the last is the logical information that defines the field as the registration page. You can add as many parameters as you like, such as placeholder and required information.

2. To add new HTML form elements for frontend registration page

3. To complete validation in the frontend registration form fields

4. To save a frontend custom field value, if there is no error

Pic : custom field added to standard register page
How do I add my own custom fields to the WordPress registration form ?

5. To add new HTML form elements for administrator user registration page

6. To complete validation in the administrator user registration form fields

7.To save a administrator user custom field value, if there is no error

Pic : Custom field added to admin register page
How do I add my own custom fields to the WordPress registration form ?

8. To add new HTML form elements for user profile page

9. To complete validation in the user profile form fields

10. To update a user profile custom field value, if there is no error

Pic : Custom field added to admin user profile page
How do I add my own custom fields to the WordPress registration form ?

Finally, Open your current active theme’s functions.php file and drop this PHP code in or create a plugin and place, here  the sample create file named as “custom_registration_fields.php” and create plugin header like below after that drop this PHP code in.

Conclusion :
Only a few lines in the code could add the required registration fields and learn a lot in the process. Some hooks are useful independently and allow further limitation of registration criteria based on existing fields, you can download full code here.

Leave a Reply

Your email address will not be published. Required fields are marked *