get_usermeta

Retrieve user metadata.

Description

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

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.

Parameters (2)

0. $user_id (int)
The user id.
1. $meta_key — Optional. (string) => ''
Metadata key.

Usage

  1. if ( !function_exists( 'get_usermeta' ) ) { 
  2. require_once ABSPATH . WPINC . '/deprecated.php'; 
  3.  
  4. // The user id. 
  5. $user_id = -1; 
  6.  
  7. // Optional. Metadata key. 
  8. $meta_key = ''; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = get_usermeta($user_id, $meta_key); 
  12.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/deprecated.php  
  1. function get_usermeta( $user_id, $meta_key = '' ) { 
  2. _deprecated_function( __FUNCTION__, '3.0.0', 'get_user_meta()' ); 
  3. global $wpdb; 
  4. $user_id = (int) $user_id; 
  5.  
  6. if ( !$user_id ) 
  7. return false; 
  8.  
  9. if ( !empty($meta_key) ) { 
  10. $meta_key = preg_replace('|[^a-z0-9_]|i', '', $meta_key); 
  11. $user = wp_cache_get($user_id, 'users'); 
  12. // Check the cached user object 
  13. if ( false !== $user && isset($user->$meta_key) ) 
  14. $metas = array($user->$meta_key); 
  15. else 
  16. $metas = $wpdb->get_col( $wpdb->prepare("SELECT meta_value FROM $wpdb->usermeta WHERE user_id = %d AND meta_key = %s", $user_id, $meta_key) ); 
  17. } else { 
  18. $metas = $wpdb->get_col( $wpdb->prepare("SELECT meta_value FROM $wpdb->usermeta WHERE user_id = %d", $user_id) ); 
  19.  
  20. if ( empty($metas) ) { 
  21. if ( empty($meta_key) ) 
  22. return array(); 
  23. else 
  24. return ''; 
  25.  
  26. $metas = array_map('maybe_unserialize', $metas); 
  27.  
  28. if ( count($metas) == 1 ) 
  29. return $metas[0]; 
  30. else 
  31. return $metas;