Show only paid subscribers of WPMUDEV Membership 2 in BuddyPress Members Directory

BuddyPress helps to build any type of community website using WordPress.
It has lots of inbuilt features such as,

  • Members Directory
  • Member profiles
  • User groups,
  • Messaging System
  • Activity streams,

and lot more. You can find more about it from here https://buddypress.org/

In this article, we are going to customize BuddyPress Members list to show only Paid Members / Subscribers of WPMUDEV’S Membership 2 Plugin.

WordPress level: Intermediate to WordPress expert.

Prerequisites:

Creating “One Year” Standard Membership

For this article purpose, let’s create Membership called “One Year” with a price tag of 100$/Year (you may create membership duration and its price of your choice). The use case is, if a user is subscribed to this Membership then only that particular member’s profile will be listed in BuddyPress members directory.
one-year-membership

Screenshot 1: One Year Membership

Note: Membership 2 comes with Four different types of Memberships

  1. Standard Membership
  2. Dripped Content Membership.
  3. Guest Membership
  4. Default Membership

In this article, we are using standard Membership, which makes content available to Members typically paid (depends on your setup). For more details about other Membership types Please go through this Plugin site https://premium.wpmudev.org/project/membership/

After Membership setup is complete, we assign “One Year” Membership to 4 users out of 16 as show in below screenshot.

on-year-paid-membersScreenshot 2: One Year Membership assigned to 4 users

Setting Up BuddyPress Plugin for customization

buddypress-members-listScreenshot 3: BuddyPress Members Directory

As you can see there are 16 members in the above list. We want to show only paid subscribers in this list.

BuddyPress has an ajax “Order By” filter located and top right corner as shown in above screenshot. By default, it has three options for “Order By” filter,

  1. Latest Active
  2. Newest Registered
  3. Alphabetical

The first two filters i.e “Latest Active” and  “Newest Registered”, as the name suggests brings only specified users, which is a subset of all list of members. For e.g Let’s say there are 5 users which are active on the site, hence “Latest Active” filter will now bring only 5 members out of 16. But if the Paid Members are let’s say 7 out of 16, the result is not what we expect (Displaying 5 users instead of 7). There is no issue for “Alphabetical” filter since it brings all users (16 Users). Hence for this article purpose, we will hide the “order by” select box filter and will use ‘Alphabetically’ as our default filter so that the customization will not break.

So Let’s start coding the result we want.

Hide “All Members” Box and “OrderBy Filter of BuddyPress

“All Members” Box it has an id of “members-all” and  Order by dropdown menu has an id of “subnav” so we target it as follows, to hide on the page.

Copy Members Loop To Child Theme Directory

Firstly we copy the  BuddyPress – Members Loop Template from the BuddyPress Plugin. the file is named as “members-loop.php” and it displays the loop of BuddyPress members as shown in Screenshot 1.

Original Location:

WORDPRESS_INSTALLATION/plugins/buddypress/bp-templates/bp-legacy/buddypress/members/members-loop.php

New location:

WORDPRESS_INSTALLATION/themes/CHILD_THEME/buddypress/members/members-loop.php

Note: above step is really not required, but it’s good idea to copy “members-loop.php” into child theme so that if there are any further customization we can do it without  affecting original file.

From above code snippet, we first check if MS_Model_Relationship Class exists, which means Membership 2 Plugin is available in the plugins directory. Paste above code in “functions.php” of the child theme.

There is a filter hook called  “bp_ajax_querystring” in BuddyPress which we are using it to filter members list. To this filter hook, we attach function “my_include_membership2 _paid_users”. In this function, we make a call to another function called  “my_membership2_paid_users”  which returns only paid members. we will have a look at this function code in a moment.

The next step in  “my_include_membership2_paid_users” function is, we check if the object passed to filter is “members ” which means we are checking if we are on members screen or not, if not we just return query string. After that we check if $ms_members_user_ids variable is not empty and then we modify the query string for members loop to include only paid users alphabetically, Finally, we return to the query string which includes only paid user id’s list  (in comma separated format).

Bring Paid Users of Membership 2

As per above code snippet, we first get all users registered for this site. After that, we loop through the array of user ids, and within each loop, we get all subscriptions of the current user. MS_Model_Relationship::get_subscriptions function is an API function from Membership 2 Plugin to get all subscriptions of a user.

Next, we check if the $subscriptions variable is an array if it’s not an array then we just continue the loop. Further, we loop through all subscriptions. Firstly we check if the subscription status is “System”, if yes then we continue with the loop.

Note: Even I am confused about this “System Membership”. I think Membership 2 allocates “System Membership” by default to all users. But I am not sure.

After that, we check if the subscription status is Active or Trail (mode) by using class constants

  • MS_Model_Relationship::STATUS_ACTIVE
  • MS_Model_Relationship::STATUS_TRIAL

If the status is active or trial we append $ms_members_user_ids string with that current user, separated by comma.

After looping through all the users, we get a comma separated list, finally, we remove the last user’s comma by using PHP’s inbuilt rtrim function and then we return paid members users string to the calling function.

buddypress-only-paid-member-list-membership2Screenshot 4: Only Paid Users with “One Year” Membership in the list

That’s it. if you now refresh, we see only 4 members which are subscribed to One Year Membership.

 

Leave a Reply