bp_core_get_user_displaynames

Fetch the display name for a group of users.

Description

(array) bp_core_get_user_displaynames( (array) $user_ids ); 

Uses the Name field in xprofile if available. Falls back on WP display_name, and then user_nicename.

Returns (array)

Parameters (1)

0. $user_ids (array)
Array of user IDs to get display names for.

Usage

  1. if ( !function_exists( 'bp_core_get_user_displaynames' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'buddypress/bp-members/bp-members-functions.php'; 
  3.  
  4. // Array of user IDs to get display names for. 
  5. $user_ids = array(); 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = bp_core_get_user_displaynames($user_ids); 
  9.  

Defined (1)

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

/bp-members/bp-members-functions.php  
  1. function bp_core_get_user_displaynames( $user_ids ) { 
  2.  
  3. // Sanitize. 
  4. $user_ids = wp_parse_id_list( $user_ids ); 
  5.  
  6. // Remove dupes and empties. 
  7. $user_ids = array_unique( array_filter( $user_ids ) ); 
  8.  
  9. if ( empty( $user_ids ) ) { 
  10. return array(); 
  11.  
  12. $uncached_ids = array(); 
  13. foreach ( $user_ids as $user_id ) { 
  14. if ( false === wp_cache_get( 'bp_user_fullname_' . $user_id, 'bp' ) ) { 
  15. $uncached_ids[] = $user_id; 
  16.  
  17. // Prime caches. 
  18. if ( ! empty( $uncached_ids ) ) { 
  19. if ( bp_is_active( 'xprofile' ) ) { 
  20. $fullname_data = BP_XProfile_ProfileData::get_value_byid( 1, $uncached_ids ); 
  21.  
  22. // Key by user_id. 
  23. $fullnames = array(); 
  24. foreach ( $fullname_data as $fd ) { 
  25. if ( ! empty( $fd->value ) ) { 
  26. $fullnames[ intval( $fd->user_id ) ] = $fd->value; 
  27.  
  28. // If xprofiledata is not found for any users, we'll look 
  29. // them up separately. 
  30. $no_xprofile_ids = array_diff( $uncached_ids, array_keys( $fullnames ) ); 
  31. } else { 
  32. $fullnames = array(); 
  33. $no_xprofile_ids = $user_ids; 
  34.  
  35. if ( ! empty( $no_xprofile_ids ) ) { 
  36. // Use WP_User_Query because we don't need BP information. 
  37. $query = new WP_User_Query( array( 
  38. 'include' => $no_xprofile_ids,  
  39. 'fields' => array( 'ID', 'user_nicename', 'display_name', ),  
  40. 'count_total' => false,  
  41. 'blog_id' => 0,  
  42. ) ); 
  43.  
  44. foreach ( $query->results as $qr ) { 
  45. $fullnames[ $qr->ID ] = ! empty( $qr->display_name ) ? $qr->display_name : $qr->user_nicename; 
  46.  
  47. // If xprofile is active, set this value as the 
  48. // xprofile display name as well. 
  49. if ( bp_is_active( 'xprofile' ) ) { 
  50. xprofile_set_field_data( 1, $qr->ID, $fullnames[ $qr->ID ] ); 
  51.  
  52. foreach ( $fullnames as $fuser_id => $fname ) { 
  53. wp_cache_set( 'bp_user_fullname_' . $fuser_id, $fname, 'bp' ); 
  54.  
  55. $retval = array(); 
  56. foreach ( $user_ids as $user_id ) { 
  57. $retval[ $user_id ] = wp_cache_get( 'bp_user_fullname_' . $user_id, 'bp' ); 
  58.  
  59. return $retval;