Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ipokkel/1fa35ed79e46c39d106373e98808d28f to your computer and use it in GitHub Desktop.
Save ipokkel/1fa35ed79e46c39d106373e98808d28f to your computer and use it in GitHub Desktop.
Hide and exclude all non-members from the BuddyPress Members #pmpro #buddypress #pmpro-buddypress
<?php
/**
* This recipe hides all non-members from the BuddyPress Members.
*
* If PMPro BuddyPress Integration active it will respect the
* per membership level access set by the integration.
*
* You can add this recipe to your site by creating a custom plugin
* or using the Code Snippets plugin available for free in the WordPress repository.
* Read this companion article for step-by-step directions on either method.
* https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
*/
// Get user ID's of all active members.
function my_pmpro_member_ids() {
global $wpdb, $pmpro_levels;
if ( ! function_exists( 'pmpro_getAllLevels' ) ) {
return array();
}
$pmpro_levels = pmpro_getAllLevels( true, true );
if ( empty( $pmpro_levels ) ) {
return array();
}
$sql = "SELECT DISTINCT user_id FROM $wpdb->pmpro_memberships_users WHERE status = 'active'";
$wpdb->flush();
$pmpro_members = $wpdb->get_col( $sql );
if ( empty( $pmpro_members ) || count( $pmpro_members ) === 0 ) {
return array();
}
return $pmpro_members;
}
// Inspired by https://buddydev.com/hiding-users-on-buddypress-based-site/
function my_pmpro_exclude_non_members_from_buddypress( $args ) {
// do not exclude in admin.
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) {
return $args;
}
$excluded = isset( $args['exclude'] ) ? $args['exclude'] : array();
if ( ! is_array( $excluded ) ) {
$excluded = explode( ',', $excluded );
}
$user_ids = array();
// uncomment and fill with actual numeric user ids.
// $user_ids = array( 1, 2, 3 ); // user ids to exclude
// Exclude users that does not have a PMPro membership level.
if ( function_exists( 'pmpro_hasMembershipLevel' ) ) {
$all_users = get_users( array( 'fields' => 'ID' ) );
if ( function_exists( 'pmpro_bp_get_members_in_directory' ) ) {
global $pmpro_bp_members_in_directory;
// remove active_members from all_users array.
$non_members = array_diff( $all_users, $pmpro_bp_members_in_directory );
} elseif ( function_exists( 'my_pmpro_member_ids' ) ) {
$pmpro_members = my_pmpro_member_ids();
$non_members = array_diff( $all_users, $pmpro_members );
} else {
$non_members = array();
}
// return numerical (integer) values in array.
$non_members = array_map( 'intval', $non_members );
// add non-members to excluded user_ids
$user_ids = array_merge( $user_ids, $non_members );
}
// array of user ids to exclude.
$excluded = array_merge( $excluded, $user_ids );
// clean out possible duplicates
$excluded = array_unique( $excluded );
// clean empty values
$excluded = array_filter( $excluded );
$args['exclude'] = $excluded;
return $args;
}
add_filter( 'bp_after_has_members_parse_args', 'my_pmpro_exclude_non_members_from_buddypress' );
@contemplate
Copy link

Worked perfectly on a Buddyboss site too! Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment