Warning: This function has been deprecated. Use get_user_meta() instead.

get_usermeta() WordPress Function

The get_usermeta() function is used to retrieve user metadata from the WordPress database. This function can be used to retrieve any metadata associated with a user, including custom fields.

get_usermeta( int $user_id, string $meta_key = '' ) #

Retrieve user metadata.


Description

If $user_id is not a number, then the function will fail over with a ‘false’ boolean return value. Other returned values depend on whether there is only one item to be returned, which be that single item type. If there is more than one metadata value, then it will be list of metadata values.

Top ↑

See also


Top ↑

Parameters

$user_id

(int)(Required)User ID

$meta_key

(string)(Optional) Metadata key.

Default value: ''


Top ↑

Return

(mixed)


Top ↑

Source

File: wp-includes/deprecated.php

function get_usermeta( $user_id, $meta_key = '' ) {
	_deprecated_function( __FUNCTION__, '3.0.0', 'get_user_meta()' );
	global $wpdb;
	$user_id = (int) $user_id;

	if ( !$user_id )
		return false;

	if ( !empty($meta_key) ) {
		$meta_key = preg_replace('|[^a-z0-9_]|i', '', $meta_key);
		$user = wp_cache_get($user_id, 'users');
		// Check the cached user object.
		if ( false !== $user && isset($user->$meta_key) )
			$metas = array($user->$meta_key);
		else
			$metas = $wpdb->get_col( $wpdb->prepare("SELECT meta_value FROM $wpdb->usermeta WHERE user_id = %d AND meta_key = %s", $user_id, $meta_key) );
	} else {
		$metas = $wpdb->get_col( $wpdb->prepare("SELECT meta_value FROM $wpdb->usermeta WHERE user_id = %d", $user_id) );
	}

	if ( empty($metas) ) {
		if ( empty($meta_key) )
			return array();
		else
			return '';
	}

	$metas = array_map('maybe_unserialize', $metas);

	if ( count($metas) == 1 )
		return $metas[0];
	else
		return $metas;
}


Top ↑

Changelog

Changelog
VersionDescription
3.0.0Use get_user_meta()
2.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