The update_user_meta()
function in WordPress is designed to modify the metadata of a user stored in the WordPress database. It is particularly handy for adjusting user-specific details like custom fields, preferences, or any other supplementary data linked to a user.
Using the update_user_meta()
function, developers have a straightforward method to alter and refresh user metadata without the need to directly interact with the WordPress database. This feature offers a safe and efficient approach to handling user-specific information within WordPress.
The update_user_meta() Function
Let’s take a look at the update_user_meta() function:
<?php
update_user_meta(int $user_id, string $meta_key, mixed $meta_value, mixed $prev_value = '')
Let’s take a look at the function parameters:
$user_id
(int): The ID of the user whose metadata will be updated.$meta_key
(string): The key of the metadata being updated.$meta_value
(mixed): The new value to be assigned to the metadata key.$prev_value
(mixed): Optional. The previous value to check before updating. If specified, only updates if the existing value matches.
Return
Int|true|false
. Meta ID if the key didn’t exist, true on successful update, false on failure or if the value passed to the function is the same as the one that is already in the database.
Practical Applications
Now that we understand the syntax and parameters of
, let’s explore some practical applications.update_user_meta()
1. Updating A Custom User Field
The update_user_meta()
is commonly used to manage user profiles in WordPress. For instance, a plugin or custom code snippet might utilize this function to allow users to update their profile information such as bio, social media links, or profile pictures.
<?php
// Get current user ID
$user_id = get_current_user_id();
update_user_meta($user_id, 'user_bio', 'A passionate WordPress enthusiast');
2. Updating A List Of User Favorite Actors
The update_user_meta()
function can also be used to update a list of favorite actors of the current user:
<?php
// Get current user ID
$user_id = get_current_user_id();
// List of actors
$actors = ['Tom Cruise', 'Brad Pitt', 'Ryan Gosling'];
update_user_meta($user_id, 'favorite_actors', $actors);
// Retreive a list of actors
$favorite_actors = get_user_meta($user_id, 'favorie_actors', true);
// result - ['Tom Cruise', 'Brad Pitt', 'Ryan Gosling']
3. Update A List Of Meta Values
We can also use update_user_meta()
function to multiple meta values at once in a loop:
<?php
// Get current user ID
$user_id = get_current_user_id();
$meta_values = [
'favorite_color' => 'Blue',
'favorite_sport' => 'Hockey'
];
foreach ($meta_values as $meta_key => $meta_value) {
update_user_meta($user_id, $meta_key, $meta_value);
}
Similar Functions
- get_user_meta( int $user_id, string $key = ”, bool $single = false ) – the get_user_meta function retrieves a meta value for a given user
- add_user_meta( int $user_id, string $meta_key, mixed $meta_value, bool $unique = false ) – Add
- delete_user_meta(int $user_id, string $meta_key, mixed $meta_value = ”) – Removes metadata matching criteria from a user.
Conclusion
The update_user_meta()
gives developers a handy way to update custom meta data of WordPress users. Give it a try in your work.