Displaying BuddyPress Extended Profile (xProfile) Data

Getting Extended Profile Data

Using bp_get_profile_data(),  extended profile field data can be retrieved to display it in theme / template file. At the minimum, this function requires user_id and field (id or name). It can be used like :

bp_get_profile_field_data($args)

like

//Usage :  bp_get_profile_field_data
$args = array(
	'field' => 5, // Field ID or name.
	'user_id' => bp_displayed_user_id() // Default -- It is profile owner id
);
$favorite_color = bp_get_profile_field_data($args);
echo $favorite_color;

Displaying Fields depending upon Visibility Setting

But problem here is, this function returns all the profile data whenever available without respecting the visibility settings of a particular profile field.

To overcome this problem, following function can used:

bp_xprofile_get_hidden_fields_for_user($displayed_user_id, $current_user_id)

The bp_xprofile_get_hidden_fields_for_user returns IDs of all profile fields which are hidden for current user.

So, complete code will be:

//Complete code
$hidden_fields = bp_xprofile_get_hidden_fields_for_user($displayed_user_id, $current_user_id);
$field_id = 5;

if(!in_array($field_id, $hidden_fields)){
  $args = array(
	'field' => $field_id, // Field ID or name.
	'user_id' => bp_displayed_user_id() // Default -- It is profile owner id
  );
  $favorite_color = bp_get_profile_field_data($args);
  echo $favorite_color;
}