In a recent project, I had to create a custom BuddyPress member list. I had to display members by their membership. So, I wanted to write a simple tutorial in which I’ll show you how to display members by their roles. In this article, you’ll learn how to create a custom member tab and how to filter only the users we want.
This tutorial will also require some customization of the BuddyPress Member list template, but I’ll show you that at the end of this article.
In this tutorial, we will show users that have the Administrator role, but you can easily copy or edit the code to show other users.
You can put this code in your theme or in a plugin. For this projects, which was a specific theme, I have put this in a child theme.
Getting the User IDs
Let’s define the function that will return an array of user IDs that we can then use for filtering.
In this function, we are also using transients. That way, we won’t have to make database queries that would take more time. That will be done only once every 12 hours. You can change that if you want.
To get the users, we are using the function
get_users(). You can read more about that function on the WordPress Codex. So, to get only the array of user IDs, we just have to define the parameter
fields and use the
ID. You may add other fields, but as long as the value of that parameter, is not an array, you’ll get user IDs.
We are also defining another parameter
role__in. This parameter will limit the query to only the users that have the role
Adding a new Tab to the BuddyPress Members Directory
By using the filter
bp_members_directory_member_types, we can add our own tabs on the BuddyPress members directory page. To create a linked tab, we will also use the function
The arguments we pass to the function
id– Description of the button type
component– Name of the component this button belongs to. Our component is
link_text– Text of the link
titleof the link
link_class– Classes added to the link
link_href– URL. We are setting the query string
show. We will use that query string to filter the user query
wrapper– If set, that element will wrap the button. Example
true, the button will be hidden if the user is viewing his/her profile
true, the button will show only if the user is logged in.
We are also putting the class
current when we are on the directory page and the query string is set to
Filtering the BuddyPress Members Directory by Role
We now have both the button and also the function to get the user IDs that we want to show. We can filter the BuddyPress member directory by overwriting the arguments that are passed when getting the users. That filter is
By passing the user IDs that we get from our function
bp_get_only_administrators_ids into the parameter
$r, we are telling to BuddyPress, that we want only users with those IDs.
Changing the Default Members Tab
selected and thus, it could confuse your visitors.
You can do that by overriding the BuddyPress templates. For this part, you just need to have a folder buddypress inside your theme. Inside that folder, another one called members. Inside that folder, you need to copy the
index.php from the BuddyPress folder bp-templates/bp-legacy/buddypress/members/index.php.
Then do this:
Just find the
class="item-list-tabs" and then change the class & remove the ID from the first
LI element. That should do the trick.
Creating custom BuddyPress directory is really easy once you understand the WordPress Plugin API. We could filter the members by changing the SQL Query directly since we also have filters for that. But that could be another tutorial. If you would like to see it, just tell me in the comments below.
Have you ever worked with BuddyPress? Be sure to share your experience below.
Want to learn more about WordPress Development? I have a course that you might want: Become a WordPress Developer.