bp_get_member_profile_data

Get a piece of user profile data.

Description

(string|bool) bp_get_member_profile_data( (string) $args = '' ); 

When used in a bp_has_members() loop, this function will attempt to fetch profile data cached in the template global. It is also safe to use outside of the loop.

Returns (string|bool)

Profile data if found, otherwise false.

Parameters (1)

0. $args — Optional. (string) => ''
Array of config parameters.

Options

  • field (string) => ''

    Name of the profile field.

array(

    /**
     * Name of the profile field.
     *
     * @type string
     * @default ''
     */
    'field' => ''
);        


Usage

  1. if ( !function_exists( 'bp_get_member_profile_data' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'buddypress/bp-members/bp-members-template.php'; 
  3.  
  4. // Array of config parameters. 
  5. $args = array( 
  6. 'field' => '' 
  7. ); 
  8.  
  9. // NOTICE! Understand what this does before running. 
  10. $result = bp_get_member_profile_data($args); 
  11.  

Defined (1)

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

/bp-members/bp-members-template.php  
  1. function bp_get_member_profile_data( $args = '' ) { 
  2. global $members_template; 
  3.  
  4. if ( ! bp_is_active( 'xprofile' ) ) { 
  5. return false; 
  6.  
  7. // Declare local variables. 
  8. $data = false; 
  9.  
  10. // Guess at default $user_id. 
  11. $default_user_id = 0; 
  12. if ( ! empty( $members_template->member->id ) ) { 
  13. $default_user_id = $members_template->member->id; 
  14. } elseif ( bp_displayed_user_id() ) { 
  15. $default_user_id = bp_displayed_user_id(); 
  16.  
  17. $defaults = array( 
  18. 'field' => false,  
  19. 'user_id' => $default_user_id,  
  20. ); 
  21.  
  22. $r = wp_parse_args( $args, $defaults ); 
  23.  
  24. // If we're in a members loop, get the data from the global. 
  25. if ( ! empty( $members_template->member->profile_data ) ) { 
  26. $profile_data = $members_template->member->profile_data; 
  27.  
  28. // Otherwise query for the data. 
  29. if ( empty( $profile_data ) && method_exists( 'BP_XProfile_ProfileData', 'get_all_for_user' ) ) { 
  30. $profile_data = BP_XProfile_ProfileData::get_all_for_user( $r['user_id'] ); 
  31.  
  32. // If we're in the members loop, but the profile data has not 
  33. // been loaded into the global, cache it there for later use. 
  34. if ( ! empty( $members_template->member ) && empty( $members_template->member->profile_data ) ) { 
  35. $members_template->member->profile_data = $profile_data; 
  36.  
  37. // Get the data for the specific field requested. 
  38. if ( ! empty( $profile_data ) && ! empty( $profile_data[ $r['field'] ]['field_type'] ) && ! empty( $profile_data[ $r['field'] ]['field_data'] ) ) { 
  39. $data = xprofile_format_profile_field( $profile_data[ $r['field'] ]['field_type'], $profile_data[ $r['field'] ]['field_data'] ); 
  40.  
  41. /** 
  42. * Filters resulting piece of member profile data. 
  43. * @since 1.2.0 
  44. * @since 2.6.0 Added the `$r` parameter. 
  45. * @param string|bool $data Profile data if found, otherwise false. 
  46. * @param array $r Array of parsed arguments. 
  47. */ 
  48.  
  49. /** 
  50. * Filters the resulting piece of member profile data by field type. 
  51. * This is a dynamic filter based on field type of the current field requested. 
  52. * @since 2.7.0 
  53. * @param string|bool $data Profile data if found, otherwise false. 
  54. * @param array $r Array of parsed arguments. 
  55. */ 
  56.  
  57. return $data;