WordPress.org

Codex

Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Function Reference/update user meta

Description

Update user meta field based on user ID.

Use the $prev_value parameter to differentiate between meta fields with the same key and user ID.

If the meta field for the user does not exist, it will be added.

Usage

<?php update_user_meta$user_id$meta_key$meta_value$prev_value ); ?>

Parameters

$user_id
(integer) (required) User ID.
Default: None
$meta_key
(string) (required) The meta_key in the wp_usermeta table for the meta_value to be updated.
Default: None
$meta_value
(mixed) (required) The new desired value of the meta_key, which must be different from the existing value. Arrays and objects will be automatically serialized. Note that using objects may cause this bug to popup.
Default: None
$prev_value
(mixed) (optional) Previous value to check before removing.
Default: ''

Return Values

(int/boolean) 
Meta ID if the key didn't exist; true on successful update; false on failure or if $meta_value is the same as the existing meta value in the database.

Examples

Below is an example showing how to update a user's Website profile field

<?php
$user_id = 1;
$website = 'http://wordpress.org';
update_user_meta($user_id, 'user_url', $website);
?>

Below is an example showing how to check for errors:

$user_id = 1;
$new_value = 'some new value';

// will return false if the previous value is the same as $new_value
update_user_meta( $user_id, 'some_meta_key', $new_value );

// so check and make sure the stored value matches $new_value
if ( get_user_meta($user_id,  'some_meta_key', true ) != $new_value )
	wp_die('An error occurred');
?>

Notes

Changes in behavior from the now deprecated update_usermeta:

  • update_user_meta does not delete the meta if the new value is empty.
  • The actions are different.

Change Log

Since: 3.0.0

Source File

update_user_meta() is located in wp-includes/user.php.

Related

add_user_meta(), delete_user_meta(), get_user_meta(), update_user_meta(), get_user_option(), delete_user_option(), update_user_option(),

See also index of Function Reference and index of Template Tags.