Home › Forums › Legacy Support › Support queries › Other issues › hide admin and student role from BP members directory
Tagged: buddypress
- This topic has 6 replies, 2 voices, and was last updated 5 years, 8 months ago by David.
Viewing 7 posts - 1 through 7 (of 7 total)
-
AuthorPosts
-
April 24, 2019 at 8:18 am #205999DavidSpectatorHello everyone, I hope you're doing well. I am trying to hide admin and student roles from the Buddypress members directory. I have tried many codes but they didn't work. Am I missing something on this code because it seems to work for many people. Are the admin role called 'administrator' and the student role 'student' ? Here is the snippet code if it can help : function get_user_ids_by_role($role){ $users=array(); $founded_users = get_users( array( 'role' => $role ) ); if(!empty($founded_users)){ foreach((array)$founded_users as $user) $users[]=$user->ID; } return $users; } function exclude_by_role( $exclude_roles, $implode = true ) { $memberArray = array(); foreach ( $exclude_roles as $exclude_role ) { $memberArray = array_merge($memberArray, get_user_ids_by_role($exclude_role) ); } if( !$implode ) return $memberArray; $theExcludeString = implode( ",", $memberArray ); return $theExcludeString; } add_action('bp_ajax_querystring','bp_exclude_byroles',20, 2); function bp_exclude_byroles($qs=false,$object=false){ if($object!='members') //hide for members only return $qs; $args = wp_parse_args($qs); //check if we are searching for friends list etc?, do not exclude in this case if(!empty($args['user_id'])) return $qs; $excluded_roles = array( 'administrator', 'student' ); // you can add roles here $exclude = exclude_by_role( $excluded_roles ); if(!empty($args['exclude'])) $args['exclude']=$args['exclude'].','.$exclude; else $args['exclude'] = $exclude; $qs = build_query($args); return $qs; } Thank you!! DavidApril 24, 2019 at 8:51 am #206005DavidSpectatorThis reply has been marked as private.April 25, 2019 at 2:55 pm #206279DavidSpectatorHello, Is there any answer ? Thanks.April 26, 2019 at 12:51 pm #206419MkModeratorThis reply has been marked as private.April 26, 2019 at 12:53 pm #206420MkModeratorFor me this code is working fine : function get_user_ids_by_role($role){ $users=array(); $founded_users = get_users( array( 'role' => $role ) ); if(!empty($founded_users)){ foreach((array)$founded_users as $user) $users[]=$user->ID; } return $users; } function exclude_by_role( $exclude_roles, $implode = true ) { $memberArray = array(); foreach ( $exclude_roles as $exclude_role ) { $memberArray = array_merge($memberArray, get_user_ids_by_role($exclude_role) ); } if( !$implode ) return $memberArray; $theExcludeString = implode( ",", $memberArray ); return $theExcludeString; } add_action('bp_ajax_querystring','bp_exclude_byroles',20, 2); function bp_exclude_byroles($qs=false,$object=false){ if($object!='members') //hide for members only return $qs; $args = wp_parse_args($qs); //check if we are searching for friends list etc?, do not exclude in this case if(!empty($args['user_id'])) return $qs; $excluded_roles = array( 'administrator', 'student' ); // you can add role's slug here $exclude = exclude_by_role( $excluded_roles ); if(!empty($args['exclude'])) $args['exclude']=$args['exclude'].','.$exclude; else $args['exclude'] = $exclude; $qs = build_query($args); return $qs; }April 26, 2019 at 12:59 pm #206421MkModeratorOne more thing , while using above code please confirm your role's slugs. You can check slugs here : http://prntscr.com/nh7cr5April 26, 2019 at 6:24 pm #206469DavidSpectatorThank you so much! The issue is resolved thanks to your clear explanation. Greetings :)
-
AuthorPosts
Viewing 7 posts - 1 through 7 (of 7 total)
- The topic ‘hide admin and student role from BP members directory’ is closed to new replies.