Retrieves user meta field for a user.
Parameters
$user_idintrequired- User ID.
$keystringoptional- The meta key to retrieve. By default, returns data for all keys.
Default:
'' $singlebooloptional- Whether to return a single value.
This parameter has no effect if$keyis not specified.
Default:
false
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 array if a valid but non-existing user ID is passed and
$single is false.An empty string if a valid but non-existing user ID is passed and
$single is true.Note: Non-serialized values are returned as strings:
- false values are returned as empty strings (
'') - true values are returned as
'1' - numbers (both integer and float) are returned as strings Arrays and objects retain their original type.
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.
Source
function get_user_meta( $user_id, $key = '', $single = false ) {
return get_metadata( 'user', $user_id, $key, $single );
}
Related
| Uses | Description |
|---|---|
get_metadata()wp-includes/meta.php | Retrieves the value of a metadata field for the specified object type and ID. |
| Used by | Description |
|---|---|
WP_Application_Passwords::get_user_application_passwords()wp-includes/class-wp-application-passwords.php | Gets a user’s application passwords. |
wp_initialize_site()wp-includes/ms-site.php | Runs the initialization routine for a given site. |
wp_default_packages_inline_scripts()wp-includes/script-loader.php | Adds inline scripts required for the WordPress JavaScript packages. |
the_block_editor_meta_boxes()wp-admin/includes/post.php | Renders the meta boxes forms. |
register_and_do_post_meta_boxes()wp-admin/includes/meta-boxes.php | Registers the default post meta boxes, and runs the |
wp_user_personal_data_exporter()wp-includes/user.php | Finds and exports personal data associated with an email address from the user and user_meta table. |
WP_User::get_caps_data()wp-includes/class-wp-user.php | Gets the available user capabilities data. |
WP_Widget_Text::render_control_template_scripts()wp-includes/widgets/class-wp-widget-text.php | Renders form template scripts. |
WP_Screen::render_meta_boxes_preferences()wp-admin/includes/class-wp-screen.php | Renders the meta boxes preferences. |
WP_User_Meta_Session_Tokens::get_sessions()wp-includes/class-wp-user-meta-session-tokens.php | Retrieves all sessions of the user. |
choose_primary_blog()wp-admin/includes/ms.php | Handles the display of choosing a user’s primary site. |
new_user_email_admin_notice()wp-includes/user.php | Adds an admin notice alerting the user to check for confirmation request email after email address change. |
WP_Internal_Pointers::enqueue_scripts()wp-admin/includes/class-wp-internal-pointers.php | Initializes the new feature pointers. |
wp_ajax_save_user_color_scheme()wp-admin/includes/ajax-actions.php | Handles auto-saving the selected color scheme for a user’s own profile via AJAX. |
wp_ajax_dismiss_wp_pointer()wp-admin/includes/ajax-actions.php | Handles dismissing a WordPress pointer via AJAX. |
WP_User::__get()wp-includes/class-wp-user.php | Magic method for accessing custom fields. |
wp_update_user()wp-includes/user.php | Updates a user in the database. |
get_blogs_of_user()wp-includes/user.php | Gets the sites a user belongs to. |
is_user_member_of_blog()wp-includes/user.php | Finds out whether a user is a member of a given blog. |
get_active_blog_for_user()wp-includes/ms-functions.php | Gets one of a user’s active blogs. |
add_user_to_blog()wp-includes/ms-functions.php | Adds a user to a blog, along with specifying the user’s role. |
remove_user_from_blog()wp-includes/ms-functions.php | Removes a user from a blog. |
Changelog
| Version | Description |
|---|---|
| 3.0.0 | Introduced. |
If the key does not exist the function will return an empty string or an empty array depending on the value of the $single parameter
falseif! is_numeric( $user_id )or! absint( $user_id ).Getting all meta data
This example demonstrates leaving the
$keyargument blank, in order to retrieve all meta data for the given user (in this example,user_id = 9):Results:
Array ( [first_name] => Array ( [0] => Tom ) [last_name] => Array ( [0] => Auger)[nickname] => Array ( [0] => tomauger ) [description] => etc.... )
Note: In order to access the data in this example you need to dereference the array that is returned for each key, like so:
To avoid this, you may want to run a simple
array_map()on the results ofget_user_meta()in order to take only the first index of each result (thus emulating what the$singleargument does when$keyis provided:Results:
Array ( [first_name] => Tom [last_name] => Auger [nickname] => tomauger [description] => etc.... )Additionally, if you want to return ALL meta for a specific user and filter out empty values, you can run
array_filter()on the results of thearray_map()above:This example returns and then displays the last name for user id 9.
Result:
The last_name value for user id 9 is Franklin
Note: When doing
get_user_meta( $user_id );without a meta key, the values are not unserialized automatically. You will need tomaybe_unserialize()them.To check if returned value is empty, ie does not exist, you could use something like: