update_usermeta( int $user_id, string $meta_key, mixed $meta_value ): bool
- Since
- 2.0.0
- Source
wp-includes/deprecated.php:2332
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.
Parameters
$user_idint- User ID
$meta_keystring- Metadata key.
$meta_valuemixed- Metadata value.
Return
bool- True on successful update, false on failure.
Hooks fired · 3
3 hooks fire while update_usermeta() runs, in this order:
- do_action( update_usermeta )actionline 2351 (+19 into the body)
- do_action( added_usermeta )actionline 2364 (+32 into the body)
- do_action( updated_usermeta )actionline 2366 (+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
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;}History
Introduced in 2.0.0. Unchanged from 6.7.7 through 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 6.8.8 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.