add-additional-fields-user-profile-wordpress

How to add additional fields to user profile in WordPress

WordPress is the most-popular CMS and provides unlimited flexibility to users to do anything they wish.

To maintain its flexibility and maintainability WordPress provides hooks. Using these hooks we can extend the functionality and customize the interfaces as per requirement.

WordPress provides metadata to add additional information with content.

Using these meta we can add number of custom fields with any content like post, page,media,user,category,terms etc.

Here we would know that how to add an additional field to user profile?
For example we have to add highest educational qualification of users.

So we need to to add a user meta for this field. Let the field name is highest_qualification.

We will hook the user meta to the hooks related to user profile. Available hooks for user profile are:

  1. show_user_profile
  2. edit_user_profile
  3. personal_options_update
  4. edit_user_profile_update

These hooks enable user to add and update the meta field. Lets have a look on code snippets:
STEP #1: Add form field when user is going to edit the profile details:

Create a function add_user_educational_qualification and hook it with show_user_profile and edit_user_profile.

<?php
    add_action( 'show_user_profile', 'add_user_educational_qualification' );
    add_action( 'edit_user_profile', 'add_user_educational_qualification' );
    function add_user_educational_qualification( $user ) { ?>
	    
    <table class="form-table">
     <tr>
      <th>
       <label for="address"><?php _e("Highest Educational Qualification");?></label>
     </th>
     <td>
      <input type="text" name="highest_qualification" id="highest_qualification" value="<?php echo esc_attr( get_the_author_meta( 'highest_qualification', $user->ID ) ); ?>" class="regular-text" />
     <span class="description"><?php _e("Please enter your heighest Educational Qualification."); ?></span>
    </td>
 </tr>
</table>
<?php }  ?>

STEP #2 : Now we need to save this information to user_meta table , so we need the hook that are used to save or update the user profile and details.

Create a function save_user_educational_qualification and hook it with personal_options_update and edit_user_profile_update.


	add_action( 'personal_options_update', 'save_user_educational_qualification' );
	add_action( 'edit_user_profile_update', 'save_user_educational_qualification' );

	function save_user_educational_qualification( $user_id ) {
	    if ( !current_user_can( 'edit_user', $user_id ) ) { 
	        return false; 
	    }
	    update_user_meta( $user_id, 'highest_qualification', $_POST['highest_qualification'] );
	}

STEP #3: Now we need to display this information any where in the website:

We are ready with our additional field highest_qualification. We have added it and saved it to database.

WordPress provide a function highest_qualification and using this function we can get the value of meta fields.

 


<?php 
   $meta_key = 'highest_qualification'; 
   $highest_qualification = get_user_meta( $user_id, $meta_key,true); 
?>
Dhananjay

Dhananjay

I’m Dhananjay — a free-spirit with a passion for technologies and blogging.

View all posts by Dhananjay →

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.