How to Add Custom User Profile Fields in WordPress

When working on projects that require user profiles and need additional user information, one needs to create a user meta to store additional information. This is akin to creating a new post meta data was where you store new data of users and link them to your posts; user meta works exactly the same way.

The WordPress database contains a table called wp_usermeta which stores all the additional user information. This table is made of four columns; a unique ID, the user ID, meta name and meta values.

In this article, we are going to learn how to add new custom fields to a user profile and save them in the wp_usermeta table.

On the user profile page, you’ll find the following actions at the bottom of the profile:

  • edit_user_profile
  • show_user_profile

WordPress allows us to use these actions to add custom fields to the profile page. The code given below adds a new custom field to include extra user information:

This code will add three extra fields to the user profile. It will show the values of these fields only if values are saved. These values can be changed too, but how can you save changed values?

Simple; when a user profile is saved, add an action to store the new information. The save actions needed are personal_options_update and edit_user_profile_update.

As we add new fields most of them will be in an array custom[key], we can use the global $_POST[custom] variable to get the input field values from the user profile page. Now, we update the user meta information with these input fields in the foreach loop on _POST[custom] as mentioned in the code (given above). To update the user meta data, we need to use the WordPress function update_user_meta():

This update_user_meta() function covers the following four parameters:

  1. $user_id
  2. $meta_key
  3. $meta_value
  4. $prev_value

If you want to display this information within your theme, there are two functions you’ll need; you can either use the function the_author_meta( $field_name, $userID ) or the function get_the_author_meta( $field_name, $userID ). The difference between these two functions is that get_the_author_meta() will return the data, while the_author_meta() will echo the data on the screen. For example:

We can use get_user_meta ( $user_id, $field_name, TRUE ); to get the value of the field. For example, to fetch the value of fields for a user whose ID is 5:

In user profile, there is a section titled Contact Info. To add fields to this area, you can simply use the filter user_contactmethods. The code for this is given below:

This will add two fields in Contact Info section and will save automatically when the user profile is also saved. We can get the values of these fields in our theme the same way as described in the process mentioned above, by get_user_meta or get_the_author_meta or the_author_meta.

User Comments

Leave a Reply

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

    Get in Touch