update_user_meta()WP 3.0.0

Updates the meta-field of the specified user.

In addition to updating an existing field, it will also create the field if a field with that key does not already exist.

The function can be used instead of add_user_meta(). When creating a field, it calls add_user_meta() with the parameter $unique = true. This means that add_user_meta() is convenient to use only in cases where multiple fields with the same keys need to be created; in other cases, it is more convenient to use this function.

Use the $prev_value parameter when you need to determine which specific field, among several with the same keys, needs to be updated.

The function does not delete the field if an empty value is passed for the $meta_value parameter.

1 time — 0.000987 sec (slow) | 50000 times — 45.34 sec (very slow) | PHP 7.0.8, WP 4.6.1

No Hooks.

Returns

Int|true|false.

  • true - on successful update.
  • false - on failure or when a value that already exists in the database was passed for the update.
  • ID of the primary field in the meta-fields table (umeta_id) when a new field was created.

Usage

update_user_meta( $user_id, $meta_key, $meta_value, $prev_value )
$user_id(number) (required)
User ID.
$meta_key(string) (required)
Field key - the meta_key field in the wp_usermeta table, for which the meta_value should be updated.
$meta_value(string/array/number/object/boolean) (required)

New field value. The value must differ from the previous one. Objects and arrays will be automatically serialized.

If a value that is already recorded in the meta-field (the same) is specified, the function will not make unnecessary update requests for changing the old value to the same new one...

$prev_value(string/array/number/object/boolean)
Old value. It needs to be specified when a user may have multiple fields with the same keys and we need to update a specific one.
Default: ''

Examples

1

#1 Basic example

Suppose the user with ID 1 should have a meta field mood. Let's update its value or create a field if it doesn't exist.

update_user_meta( 1, 'mood', 'good' );
0

#2 Updating the user site

In the meta fields of the user we are familiar with: Nick, Name, Last Name, Site, etc. The example below shows how to update the site of the user with ID 1:

$user_id = 1;
$website = 'http://wordpress.org';
update_user_meta( $user_id, 'user_url', $website );
0

#3 Error check

This example shows how to check if the field has been updated:

$user_id = 1;
$new_value = 'good';

// It returns false if the previous value is equal to $new_value.
$updated = update_user_meta( $user_id, 'some_meta_key', $new_value );
if( ! $updated ){
	echo 'The field has not been updated';
}

// Get the value of the field and check it with the new value $new_value
$val = get_user_meta( $user_id,  'some_meta_key', true );
if( $val !== $new_value ){
	echo 'Error: no new value was written.';
}
0

#4 Note about update ALL user meta of the same key

update_user_meta() will update ALL user meta-fields with the same key, UNLESS a specific post in the table to be updated is specified.

Here’s how to do this when there are user meta-fields that may look like this:

+---------+----------------------+-----------------
| user_id | meta_key             | meta_value 
+---------+----------------------+-----------------
| 862     | favorite_coffee      | {"coffee_id":10,"title":"Vietnamese Iced Coffee","type":"drip"}
| 862     | favorite_coffee      | {"coffee_id":11,"title":"Mocha","type":"espresso"}
| 862     | favorite_coffee      | {"coffee_id":12,"title":"Just An Okay Cappuchino","type":"espresso"}
// dummy data for better illustration of the problem, 
// we want to change the title of `coffee_id` 12
$parameters = [
	'coffee_id' => '12',
	'title'     => 'A Delicious Cappuchino',
	'type'      => 'espresso',
];

// let's try to find the meta-fields ``favorite_coffee`` 
$previous_favorite_coffee = get_user_meta( $user_id, 'favorite_coffee', false );

/**
 * First, the condition when the user metadata favorite_coffee is absent
 */ 
if ( ! $previous_favorite_coffee ) {
	add_user_meta( $user_id, 'favorite_coffee', $parameters );
}

/**
 * Second, the condition when the user favorite_coffee already exists
 */
// recursive search through the posts returned by get_user_meta, 
// for the post you want to replace, as defined by `coffee_id`.
// See: http://php.net/manual/en/function.array-search.php#116635
$coffee_id = array_search( 
	$parameters['coffee_id'], 
	array_column( $previous_favorite_coffee, 'coffee_id' ) 
);

// add if the pair does not exist:
// wp_usermeta meta_key[favorite_coffee] => meta_value[ $parameters[ $coffee_id ] ] 
if ( false === $coffee_id ) {
	add_user_meta( $user_id, 'favorite_coffee', $parameters );
}
// update if the pair already exists:
// wp_usermeta meta_key[favorite_coffee] => meta_value[ $parameters[ $coffee_id ] ] 
else {
	update_user_meta( $user_id, 'favorite_coffee', $parameters, $previous_favorite_coffee[ $coffee_id ] );
}

Changelog

Since 3.0.0 Introduced.

update_user_meta() code WP 6.9.1

function update_user_meta( $user_id, $meta_key, $meta_value, $prev_value = '' ) {
	return update_metadata( 'user', $user_id, $meta_key, $meta_value, $prev_value );
}