bp_members_get_user_nicename

Return the user_nicename for a user based on their user_id.

Description

(string|bool) bp_members_get_user_nicename( (int) $user_id ); 

This should be used for linking to user profiles and anywhere else a sanitized and unique slug to a user is needed.

Returns (string|bool)

The username of the matched user, or false.

Parameters (1)

0. $user_id (int)
User ID to check.

Usage

  1. if ( !function_exists( 'bp_members_get_user_nicename' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'buddypress/bp-members/bp-members-functions.php'; 
  3.  
  4. // User ID to check. 
  5. $user_id = -1; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = bp_members_get_user_nicename($user_id); 
  9.  

Defined (1)

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

/bp-members/bp-members-functions.php  
  1. function bp_members_get_user_nicename( $user_id ) { 
  2. $bp =buddypress); 
  3.  
  4. if ( !$user_nicename = wp_cache_get( 'bp_members_user_nicename_' . $user_id, 'bp' ) ) { 
  5. $update_cache = true; 
  6.  
  7. // User ID matches logged in user. 
  8. if ( bp_loggedin_user_id() == $user_id ) { 
  9. $userdata = &$bp->loggedin_user->userdata; 
  10.  
  11. // User ID matches displayed in user. 
  12. } elseif ( bp_displayed_user_id() == $user_id ) { 
  13. $userdata = &$bp->displayed_user->userdata; 
  14.  
  15. // No user ID match. 
  16. } else { 
  17. $userdata = false; 
  18.  
  19. // No match so go dig. 
  20. if ( empty( $userdata ) ) { 
  21.  
  22. // User not found so return false. 
  23. if ( !$userdata = bp_core_get_core_userdata( $user_id ) ) { 
  24. return false; 
  25.  
  26. // User nicename found. 
  27. $user_nicename = $userdata->user_nicename; 
  28.  
  29. // Nicename found in cache so don't update it again. 
  30. } else { 
  31. $update_cache = false; 
  32.  
  33. // Add this to cache. 
  34. if ( true == $update_cache && !empty( $user_nicename ) ) { 
  35. wp_cache_set( 'bp_members_user_nicename_' . $user_id, $user_nicename, 'bp' ); 
  36.  
  37. /** 
  38. * Filters the user_nicename based on originally provided user ID. 
  39. * @since 1.5.0 
  40. * @param string $username User nice name determined by user ID. 
  41. */ 
  42. return apply_filters( 'bp_members_get_user_nicename', $user_nicename );