Working with User Metadata

The WordPress users table contains a very small amount of information about a user, and can’t hold more.  For this reason there is a usermeta table.  By default this contains things like First Name, Last Name, Nickname, etc.  Anything may be placed in there, it’s simply related via user_id.

There are two different main scenarios for managing User Meta.  One is via custom form fields on the User Profile page, and the other is programmatically with the proper functions.

Adding User Meta Via Profile Form Adding User Meta Via Profile Form

Creating the form Creating the form

If you simply wish to provide a UI for site users to update a specific meta option you can use the code below.

add_action( 'show_user_profile', 'my_show_extra_profile_fields' );
add_action( 'edit_user_profile', 'my_show_extra_profile_fields' );

function my_show_extra_profile_fields( $user ) { ?>

	<h3>Extra profile information</h3>

	<table class="form-table">

		<tr>
			<th><label for="twitter">Twitter</label></th>

			<td>
				<input type="text" name="twitter" id="twitter" value="<?php echo esc_attr( get_the_author_meta( 'twitter', $user->ID ) ); ?>" class="regular-text" /><br />
				<span class="description">Please enter your Twitter username.</span>
			</td>
		</tr>

	</table>
<?php }

Note there are two actions tags. show_user_profile is for showing the form on your own profile, and edit_user_profile is for showing it on everyone else’s.

The above code will render an html block on the profile page like this:

HTML block showing additional fields.

Additional profile fields

Top ↑

Saving the data Saving the data

Simply making the form doesn’t make the data save. To do that you should use the code below:

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

function my_save_extra_profile_fields( $user_id ) {

	if ( !current_user_can( 'edit_user', $user_id ) )
		return false;

	/* Copy and paste this line for additional fields. Make sure to change 'twitter' to the field ID. */
	update_usermeta( absint( $user_id ), 'twitter', wp_kses_post( $_POST['twitter'] ) );
}

Note that update_usermeta is used even when there is a possibility that the value doesn’t previously exist. That’s ok, it’ll create if it doesn’t exist.

Also note the two add_action functions. Similar to above one of them applies to your own profile page, and the other to everyone elses.

Much of the above text was taken from this post by Justin Tadlock.

Top ↑

Adding User Meta Programmatically Adding User Meta Programmatically

There are two ways to add user meta programmatically.  add_user_meta() and update_user_meta().

Top ↑

add_user_meta add_user_meta

<?php add_user_meta( $user_id, $meta_key, $meta_value, $unique ); ?>

Top ↑

Getting and Rendering User Meta Getting and Rendering User Meta

Getting user meta can be done all at once, and can return an array of all values, or you can get it a single value at a time.  Here’s the function:

<?php get_user_meta($user_id, $key, $single);  ?>

If you simply pass a user_id you’ll get all options in an array.

If you pass a user_id and key then you’ll get the value of that key in an array.

If you pass all, and single is true, then you’ll get the value in either a string or an int, unless the value stored is itself an array or object.

At that point you can print as you wish.