wppaste
WordPress

update_usermeta( int $user_id, string $meta_key, mixed $meta_value ): bool

Since
2.0.0
Source
wp-includes/deprecated.php:2335
Update metadata of user.

Description

There is no need to serialize values, they will be serialized if it is needed. The metadata key can only be a string with underscores. All else will be removed.

Will remove the metadata, if the meta value is empty.

Compatibility

Deprecated in WordPress 3.0.0. Use update_user_meta()

WordPress
since 2.0.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$user_idint
User ID
$meta_keystring
Metadata key.
$meta_valuemixed
Metadata value.

Return value

bool
True on successful update, false on failure.

Performance profile

How much work a call to update_usermeta() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Heavy

Reaches the database via ->get_row().

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
12–86

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 115.

Plugin surface
3 hooks

Third-party callbacks on 'update_usermeta', 'added_usermeta', 'updated_usermeta' run inside this call, and their cost is not bounded by anything here.

Called by
0

Nothing in core calls this; the cost is only what you spend yourself.

What it touches

  • serializeserialisationmaybe_serialize()called directly
  • hookthird-party callbacksdo_action()called directly
  • cacheobject cachewp_cache_delete()called directly
  • sqldatabase query->get_row()called directly

Further down the call graph this can also reach query, option and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 11 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost update_usermeta() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
!$user_id12_deprecated_function()
$user_id && !is_string($meta_value) && empty($meta_value)27_deprecated_function(), maybe_serialize(), delete_usermeta()
$user_id && is_string($meta_value) && empty($meta_value)31_deprecated_function(), stripslashes(), maybe_serialize(), delete_usermeta()
$user_id && !is_string($meta_value) && !empty($meta_value) && $meta_value == false50_deprecated_function(), maybe_serialize(), ->prepare(), ->get_row(), do_action()
$user_id && is_string($meta_value) && !empty($meta_value) && $meta_value == false54_deprecated_function(), stripslashes(), maybe_serialize(), ->prepare(), ->get_row(), do_action()
$user_id && !is_string($meta_value) && !empty($meta_value)66_deprecated_function(), maybe_serialize(), ->prepare(), ->get_row(), compact(), ->insert(), clean_user_cache(), wp_cache_delete(), do_action()
$user_id && is_string($meta_value) && !empty($meta_value)70_deprecated_function(), stripslashes(), maybe_serialize(), ->prepare(), ->get_row(), compact(), ->insert(), clean_user_cache(), wp_cache_delete(), do_action()
$user_id && !is_string($meta_value) && !empty($meta_value)76_deprecated_function(), maybe_serialize(), ->prepare(), ->get_row(), do_action(), compact(), ->insert(), clean_user_cache(), wp_cache_delete(), do_action()
$user_id && is_string($meta_value) && !empty($meta_value)80_deprecated_function(), stripslashes(), maybe_serialize(), ->prepare(), ->get_row(), do_action(), compact(), ->insert(), clean_user_cache(), wp_cache_delete(), do_action()
$user_id && !is_string($meta_value) && !empty($meta_value) && $meta_value != false82_deprecated_function(), maybe_serialize(), ->prepare(), ->get_row(), do_action(), compact(), compact(), ->update(), clean_user_cache(), wp_cache_delete(), do_action()
$user_id && is_string($meta_value) && !empty($meta_value) && $meta_value != false86_deprecated_function(), stripslashes(), maybe_serialize(), ->prepare(), ->get_row(), do_action(), compact(), compact(), ->update(), clean_user_cache(), wp_cache_delete(), do_action()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev11512–867
8.511512–867
8.411512–8675 fewer instructions than PHP 8.3
8.312014–917
8.212014–917
8.112014–917
7.412014–917

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 3

3 hooks fire while update_usermeta() runs, in this order:

  1. do_action( update_usermeta )actionline 2354 (+19 into the body)
  2. do_action( added_usermeta )actionline 2367 (+32 into the body)
  3. do_action( updated_usermeta )actionline 2369 (+34 into the body)

Uses · 6

Source code

function update_usermeta( $user_id, $meta_key, $meta_value ) {	_deprecated_function( __FUNCTION__, '3.0.0', 'update_user_meta()' );	global $wpdb;	if ( !is_numeric( $user_id ) )		return false;	$meta_key = preg_replace('|[^a-z0-9_]|i', '', $meta_key); 	/** @todo Might need fix because usermeta data is assumed to be already escaped */	if ( is_string($meta_value) )		$meta_value = stripslashes($meta_value);	$meta_value = maybe_serialize($meta_value); 	if (empty($meta_value)) {		return delete_usermeta($user_id, $meta_key);	} 	$cur = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->usermeta WHERE user_id = %d AND meta_key = %s", $user_id, $meta_key) ); 	if ( $cur )		do_action( 'update_usermeta', $cur->umeta_id, $user_id, $meta_key, $meta_value ); 	if ( !$cur )		$wpdb->insert($wpdb->usermeta, compact('user_id', 'meta_key', 'meta_value') );	elseif ( $cur->meta_value != $meta_value )		$wpdb->update($wpdb->usermeta, compact('meta_value'), compact('user_id', 'meta_key') );	else		return false; 	clean_user_cache( $user_id );	wp_cache_delete( $user_id, 'user_meta' ); 	if ( !$cur )		do_action( 'added_usermeta', $wpdb->insert_id, $user_id, $meta_key, $meta_value );	else		do_action( 'updated_usermeta', $cur->umeta_id, $user_id, $meta_key, $meta_value ); 	return true;}

Changelog

Introduced in 2.0.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

3.0.0
Deprecated. Use update_user_meta()from the docblock
2.0.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/deprecated.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.