get_user_meta() WordPress Function

The get_user_meta() function is used to retrieve custom user meta data from the WordPress usermeta table. This function can be used to get both standard and custom user meta data.

get_user_meta( int $user_id, string $key = '', bool $single = false ) #

Retrieves user meta field for a user.


Parameters

$user_id

(int)(Required)User ID.

$key

(string)(Optional) The meta key to retrieve. By default, returns data for all keys.

Default value: ''

$single

(bool)(Optional) Whether to return a single value. This parameter has no effect if $key is not specified.

Default value: false


Top ↑

Return

(mixed) An array of values if $single is false. The value of meta data field if $single is true. False for an invalid $user_id (non-numeric, zero, or negative value). An empty string if a valid but non-existing user ID is passed.


Top ↑

More Information

Please note that if the meta value exists but is empty, it will return an empty string (or array) as if the meta value didn’t exist. This might cause unexpected behaviors in your code when you empty the user meta, your code can try to use add_user_meta instead of update_user_meta thinking the user does not have meta created yet.


Top ↑

Source

File: wp-includes/user.php

function get_user_meta( $user_id, $key = '', $single = false ) {
	return get_metadata( 'user', $user_id, $key, $single );
}


Top ↑

Changelog

Changelog
VersionDescription
3.0.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.

Show More
Show More