update_usermeta( int $user_id, string $meta_key, mixed $meta_value ): bool
- Since
- 2.0.0
- Source
wp-includes/deprecated.php:2335
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
- Scaling
- Constant
- Instructions
- 12–86
- Plugin surface
- 3 hooks
- Called by
- 0
Reaches the database via ->get_row().
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 115.
Third-party callbacks on 'update_usermeta', 'added_usermeta', 'updated_usermeta' run inside this call, and their cost is not bounded by anything here.
Nothing in core calls this; the cost is only what you spend yourself.
What it touches
- serializeserialisation
maybe_serialize()called directly - hookthird-party callbacks
do_action()called directly - cacheobject cache
wp_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.
| When | Instructions | Calls it makes |
|---|---|---|
!$user_id | 12 | _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 == false | 50 | _deprecated_function(), maybe_serialize(), ->prepare(), ->get_row(), do_action() |
$user_id && is_string($meta_value) && !empty($meta_value) && $meta_value == false | 54 | _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 != false | 82 | _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 != false | 86 | _deprecated_function(), stripslashes(), maybe_serialize(), ->prepare(), ->get_row(), do_action(), compact(), compact(), ->update(), clean_user_cache(), wp_cache_delete(), do_action() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 115 | 12–86 | 7 | |
| 8.5 | 115 | 12–86 | 7 | |
| 8.4 | 115 | 12–86 | 7 | 5 fewer instructions than PHP 8.3 |
| 8.3 | 120 | 14–91 | 7 | |
| 8.2 | 120 | 14–91 | 7 | |
| 8.1 | 120 | 14–91 | 7 | |
| 7.4 | 120 | 14–91 | 7 |
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:
- do_action( update_usermeta )actionline 2354 (+19 into the body)
- do_action( added_usermeta )actionline 2367 (+32 into the body)
- do_action( updated_usermeta )actionline 2369 (+34 into the body)
Uses · 6
- _deprecated_function()Marks a function as deprecated and inform when it has been used.
- maybe_serialize()Serializes data, if needed.
- delete_usermeta()Remove user meta data.
- do_action()Calls the callback functions that have been added to an action hook.
- clean_user_cache()Cleans all user caches.
- wp_cache_delete()Removes the cache contents matching key and group.
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.
Signature, return type and hooks compared across 5 parsed releases.
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.