Adding extra fields in user profile page in WordPress admin panel

Sometimes you need to add some extra fields in user profile page in WordPress. Here is what you have to do to achieve this.

For example I am going to add cellphone number and city fields. In which cellphone number is required field with valid 10 digit cellphone number and city field is an optional field.

Step : 1- Add fields in UI

Add below code in your plugin’s or theme’s fumction.php file

 

Here we are using show_user_profile and edit_user_profile both action hooks so that the user who don’t have permission to edit user but have permission to view user profile can see new fields as well.

After adding this code in you can see a new section titled “Extra User information” in user profile page. Within this section 2 new fields has been added as you can see in image.

We are going to store these field’s value in usermeta table, to show again pre-populated fields I am using get_the_author_meta(); function.

Step : 2- Validate fields

Now you need to add validations for value provided.

 

In this example if you entered wrong phone number, fields will not update and an error message will be displayed.

Step : 3- Save field’s value

Now we need to save these field’s value in our database.

As I mentioned above, we are going to store this data in usermeta table using update_user_meta() function.

 

Here finally we saved data in our database.

If you are facing any problem please comment below.

Please follow and like us:
20