Advertisement
Guest User

Excludes users by role from the BuddyPress loop

a guest
Feb 16th, 2017
764
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.67 KB | None | 0 0
  1. /*********************************************************/
  2. /* Excludes users with role of "suspended" from the loop */
  3. /*********************************************************/
  4.  
  5. function bgmc_get_user_ids_by_role($role) {
  6.     $users = array();
  7.     $founded_users = get_users( array( 'role' => $role ) );
  8.     if ( !empty( $founded_users) ) foreach ( (array)$founded_users as $user ) $users[] = $user->ID;
  9.     return $users;
  10. }
  11.  
  12. function bgmc_exclude_by_role( $exclude_roles, $implode = true ) {
  13.     $memberArray = array();
  14.     foreach ( $exclude_roles as $exclude_role ) $memberArray = array_merge( $memberArray, bgmc_get_user_ids_by_role( $exclude_role ) );
  15.    
  16.     if ( !$implode ) return $memberArray;
  17.    
  18.     $theExcludeString = implode( ",", $memberArray );
  19.    
  20.     return $theExcludeString;
  21. }
  22.  
  23. function bgmc_exclude_byroles( $qs=false,$object=false ) {
  24.  
  25.     if ( $object!='members' ) return $qs; //hide for members only
  26.  
  27.     $args = wp_parse_args( $qs );
  28.    
  29.     if ( !empty( $args['user_id'] ) ) return $qs; //check if we are searching for friends list etc?, do not exclude in this case
  30.  
  31.     $excluded_roles = array( 'suspended' ); // you can add roles here
  32.  
  33.     $exclude = get_transient( 'excluded_members' ); // Use a transient to prevent overloading the database on sites with a lot of members
  34.    
  35.     if ( !$exclude ) :
  36.         $exclude = bgmc_exclude_by_role( $excluded_roles );
  37.         set_transient( 'excluded_members', $exclude, WEEK_IN_SECONDS );
  38.     endif;
  39.  
  40.     if ( !empty( $args['exclude'] ) ):
  41.         $args['exclude'] = $args['exclude'].','.$exclude;
  42.     else:
  43.         $args['exclude'] = $exclude;
  44.     endif;
  45.      
  46.     $qs = build_query($args);
  47.    
  48.     return $qs;
  49. }
  50. add_action( 'bp_ajax_querystring', 'bgmc_exclude_byroles', 20, 2 );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement