BuddyBuilder code snippets
These PHP snippets change BuddyPress behavior that the BuddyBuilder directory widgets have no setting for. They use standard BuddyPress filters, so they work with BuddyBuilder templates and with the default BuddyPress templates.
Where to add a snippet
Section titled “Where to add a snippet”Add the code to the functions.php file of your child theme, or to a code snippets plugin. Do not edit the parent theme or the plugin files: an update overwrites them.
- Skip the opening
<?phptag if the file already has one. - The function names start with
my_. Keep them unique: two functions with the same name stop the site with a fatal error. - Test on a staging site first when you can.
Change the number of members per page
Section titled “Change the number of members per page”The Members Listing widget of the Members Directory template has no per-page setting: the directory shows 20 members per page, the BuddyPress default. This snippet changes the number.
add_filter( 'bp_after_has_members_parse_args', 'my_bp_members_per_page' );function my_bp_members_per_page( $args ) { $args['per_page'] = 24;
return $args;}Replace 24 with your number. The filter applies to every BuddyPress members loop, including the friends list on a profile.
Change the number of groups per page
Section titled “Change the number of groups per page”The Groups Listing widget of the Groups Directory template has no per-page setting either. BuddyPress shows 20 groups per page. To change it:
add_filter( 'bp_after_has_groups_parse_args', 'my_bp_groups_per_page' );function my_bp_groups_per_page( $args ) { $args['per_page'] = 12;
return $args;}Replace 12 with your number. The filter applies to every BuddyPress groups loop, including the groups list on a member profile.
Limit group excerpts
Section titled “Limit group excerpts”Group descriptions are shortened in the groups directory and shown in full on the group profile. Both lengths can be changed. The number is the maximum length in characters; BuddyPress cuts at a word boundary and adds an ellipsis.
In the groups directory:
add_filter( 'bp_nouveau_get_group_description_excerpt', 'my_bp_group_directory_excerpt', 20, 2 );function my_bp_group_directory_excerpt( $excerpt, $group ) { return bp_create_excerpt( $group->description, 80 );}On the group profile (the Description widget of the Group Profile template):
add_filter( 'bp_get_group_description', 'my_bp_group_profile_excerpt', 20 );function my_bp_group_profile_excerpt( $description ) { if ( is_admin() || ! bp_is_group() ) { return $description; }
return bp_create_excerpt( $description, 150 );}Hide a user role from the members directory
Section titled “Hide a user role from the members directory”To keep the users of one role (administrators in this example) out of the members directory:
add_filter( 'bp_after_has_members_parse_args', 'my_bp_hide_role_from_members_directory' );function my_bp_hide_role_from_members_directory( $args ) { if ( ! bp_is_members_directory() ) { return $args; }
$hidden = get_users( array( 'role' => 'administrator', 'fields' => 'ID', ) );
$exclude = empty( $args['exclude'] ) ? array() : wp_parse_id_list( $args['exclude'] ); $args['exclude'] = array_merge( $exclude, $hidden );
return $args;}Change administrator to the role slug you want to hide, for example subscriber or editor. The users are only removed from the directory list: their profiles stay reachable by direct link, and the member count shown in the directory navigation does not change.
Show only friends’ activity in the activity stream
Section titled “Show only friends’ activity in the activity stream”To limit the site-wide activity page to the logged-in member’s own updates and those of their friends:
add_filter( 'bp_after_has_activities_parse_args', 'my_bp_friends_only_activity' );function my_bp_friends_only_activity( $args ) { if ( ! is_user_logged_in() || ! bp_is_active( 'friends' ) || ! bp_is_activity_directory() ) { return $args; }
$user_id = get_current_user_id(); $user_ids = friends_get_friend_user_ids( $user_id ); $user_ids[] = $user_id;
$args['user_id'] = $user_ids;
return $args;}The snippet needs the BuddyPress Friend Connections component. Logged-out visitors still see the full stream; activity on member and group profiles is not affected.
Related pages
Section titled “Related pages”- Members directory widgets and Groups directory widgets: the settings the widgets do have.
- BuddyBuilder troubleshooting