Adding user metadata in WordPress with add_user_meta

The WordPress add_user_meta function is used to add metadata to a user in the WordPress database. This can be useful for storing additional information about a user that is not included in the default user fields. For example, you could use it to store a user’s preferred language, their membership status, or any other custom information that is relevant to your website.

By using add_user_meta, you can easily extend the user profile with custom fields and retrieve this information later when needed. This can be particularly useful for plugin and theme developers who need to store and retrieve additional user data for their specific functionality.

Parameters accepted by the WordPress add_user_meta function

  • $user_id (int) – User ID.
  • $meta_key (string) – Metadata name.
  • $meta_value (mixed) – Metadata value. Must be serializable if non-scalar.
  • $unique (bool, optional, default: false) – Whether the same key should not be added.

Value returned by the WordPress add_user_meta function

The function returns an int representing the Meta ID on success, or false on failure.

Examples

How to add user meta in WordPress

Here’s an example of how to use the add_user_meta function to add a custom field to a user’s profile:

$user_id = 123;
$meta_key = 'favorite_color';
$meta_value = 'blue';

add_user_meta( $user_id, $meta_key, $meta_value );

This code snippet adds a user meta field with the key 'favorite_color' and the value 'blue' to the user with the ID 123.

How to update user meta in WordPress

Below is an example of using the add_user_meta function to update an existing user meta field:

$user_id = 123;
$meta_key = 'favorite_color';
$meta_value = 'green';

// Check if the meta key already exists
if ( get_user_meta( $user_id, $meta_key, true ) ) {
 update_user_meta( $user_id, $meta_key, $meta_value );
} else {
 add_user_meta( $user_id, $meta_key, $meta_value );
}

This code snippet first checks if the user meta field with the key 'favorite_color' exists for the user with the ID 123. If it does, the value is updated to 'green'; otherwise, a new meta field is added with the key 'favorite_color' and the value 'green'.

How to delete user meta in WordPress

Here’s an example of using the delete_user_meta function to remove a user meta field:

$user_id = 123;
$meta_key = 'favorite_color';

delete_user_meta( $user_id, $meta_key );

This code snippet deletes the user meta field with the key 'favorite_color' from the user with the ID 123.

Conclusion

In conclusion, the add_user_meta function is a valuable component for adding custom metadata to user profiles in WordPress. This function allows developers to store additional information about users, which can be useful for various purposes such as user management, personalization, and data analysis. By using add_user_meta, developers can easily extend the default user profile fields and create a more tailored user experience. With its simple syntax and flexibility, this function is a valuable resource for WordPress developers looking to enhance their websites with custom user data.

Related WordPress Functions