bp_core_get_username

Return the username for a user based on their user id.

Description

(string|bool) bp_core_get_username( (int) $user_id = 0, (constant) $user_nicename = false, (bool) $user_login = false ); 

This function is sensitive to the BP_ENABLE_USERNAME_COMPATIBILITY_MODE, so it will return the user_login or user_nicename as appropriate.

Returns (string|bool)

The username of the matched user, or false.

Parameters (3)

0. $user_id — Optional. (int)
User ID to check.
1. $user_nicename — Optional. (constant) => false
user_nicename of user being checked.
2. $user_login — Optional. (bool) => false
user_login of user being checked.

Usage

  1. if ( !function_exists( 'bp_core_get_username' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'buddypress/bp-members/bp-members-functions.php'; 
  3.  
  4. // User ID to check. 
  5. $user_id = -1; 
  6.  
  7. // Optional. user_nicename of user being checked. 
  8. $user_nicename = false; 
  9.  
  10. // Optional. user_login of user being checked. 
  11. $user_login = false; 
  12.  
  13. // NOTICE! Understand what this does before running. 
  14. $result = bp_core_get_username($user_id, $user_nicename, $user_login); 
  15.  

Defined (1)

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

/bp-members/bp-members-functions.php  
  1. function bp_core_get_username( $user_id = 0, $user_nicename = false, $user_login = false ) { 
  2. $bp =buddypress); 
  3.  
  4. // Check cache for user nicename. 
  5. $username = wp_cache_get( 'bp_user_username_' . $user_id, 'bp' ); 
  6. if ( false === $username ) { 
  7.  
  8. // Cache not found so prepare to update it. 
  9. $update_cache = true; 
  10.  
  11. // Nicename and login were not passed. 
  12. if ( empty( $user_nicename ) && empty( $user_login ) ) { 
  13.  
  14. // User ID matches logged in user. 
  15. if ( bp_loggedin_user_id() == $user_id ) { 
  16. $userdata = &$bp->loggedin_user->userdata; 
  17.  
  18. // User ID matches displayed in user. 
  19. } elseif ( bp_displayed_user_id() == $user_id ) { 
  20. $userdata = &$bp->displayed_user->userdata; 
  21.  
  22. // No user ID match. 
  23. } else { 
  24. $userdata = false; 
  25.  
  26. // No match so go dig. 
  27. if ( empty( $userdata ) ) { 
  28.  
  29. // User not found so return false. 
  30. if ( !$userdata = bp_core_get_core_userdata( $user_id ) ) { 
  31. return false; 
  32.  
  33. // Update the $user_id for later. 
  34. $user_id = $userdata->ID; 
  35.  
  36. // Two possible options. 
  37. $user_nicename = $userdata->user_nicename; 
  38. $user_login = $userdata->user_login; 
  39.  
  40. // Pull an audible and maybe use the login over the nicename. 
  41. $username = bp_is_username_compatibility_mode() ? $user_login : $user_nicename; 
  42.  
  43. // Username found in cache so don't update it again. 
  44. } else { 
  45. $update_cache = false; 
  46.  
  47. // Add this to cache. 
  48. if ( ( true === $update_cache ) && !empty( $username ) ) { 
  49. wp_cache_set( 'bp_user_username_' . $user_id, $username, 'bp' ); 
  50.  
  51. // @todo bust this cache if no $username found? 
  52. // } else { 
  53. // wp_cache_delete( 'bp_user_username_' . $user_id ); 
  54.  
  55. /** 
  56. * Filters the username based on originally provided user ID. 
  57. * @since 1.0.1 
  58. * @param string $username Username determined by user ID. 
  59. */ 
  60. return apply_filters( 'bp_core_get_username', $username );