Search
Close this search box.

How to exclude profile fields in BuddyPress

In BuddyPress, you can exclude specific profile fields from being displayed in member directories and member profiles using the bp_xprofile_get_hidden_fields_for_user function. This function returns an array of field IDs for the fields that are set to be hidden for a given user.

Here’s an example of how you can use this function to exclude certain fields from being displayed:

<?php

// Exclude profile fields in BuddyPress
// More snippets at wpunplugged.com 

// Get the current user's ID
$user_id = bp_displayed_user_id();

// Get an array of hidden field IDs for the user
$hidden_fields = bp_xprofile_get_hidden_fields_for_user( $user_id );

// Loop through all of the user's profile fields
$fields = bp_get_profile_field_data( array(
    'user_id' => $user_id,
    'fetch_field_data' => true
) );
foreach ( $fields as $field ) {
	// If the field is not in the array of hidden fields, display it
	if ( ! in_array( $field->id, $hidden_fields ) ) {
		echo '<p>' . $field->name . ': ' . $field->value . '</p>';
	}
}
?>

This code will display all of the user’s profile fields, except for the ones that are set to be hidden.

You can also use the bp_profile_field_data function to display a specific profile field, rather than looping through all of the fields. For example:

<?php

// Exclude profile fields in BuddyPress
// More snippets at wpunplugged.com 

// Get the field data for the "Location" field
$location = bp_profile_field_data( array(
    'field'   => 'Location',
    'user_id' => $user_id
) );

// Display the field data if it is not hidden
if ( ! in_array( $location->id, $hidden_fields ) ) {
	echo '<p>Location: ' . $location->value . '</p>';
}
?>

Share this post

You might also like...

Comment

Your email address will not be published. Required fields are marked *