BuddyDev

Search

[Resolved] How to hide users by user role from the Buddypress Directory

  • Participant
    Level: Enlightened
    Posts: 23
    Stephanie on #52569

    Hello,

    Sent en email to support earlier because I wasn’t able to log in and access the forum, but now that I am here, you can ignore the email support request.

    There is a very useful piece of code that I would really like to use. Unfortunately there is a lot discussion about it and so it is unclear what exactly we should use, and how to use it. The code is to hide or show certain user roles from the Buddypress Directory, available here: https://gist.github.com/sbrajesh/2142009

    The discussion is quite confusing especially to a non-developer and I was wondering if you could help by providing a definitive answer as to how to hide a single or a few user roles from the directory. The last code on the page in link is what I need, but I don’t know if I am supposed to use that as is, or if it replaces part of the original code (and if so, what part), and where to add the user roles I want to hide or include. It would be really great if you could update both versions (hide every role except one, and show every role except one, and tell us where exactly to put the user roles we want to hide/show in the code), it would benefit everyone.

    Thank you so much.

  • Keymaster
    (BuddyDev Team)
    Posts: 24332
    Brajesh Singh on #52574

    Hi Stephanie,
    Thank you.

    I have updated the gist with examples on how to exclude multiple roles or limit to multiple roles. Please do note that I don’t recommend excluding thousands of users using that approach. If you are trying to exclude couple hundred users, it should be fine.

    Please let me know if it works for you or not?

    Regards
    Brajesh

  • Participant
    Level: Enlightened
    Posts: 23
    Stephanie on #52576

    Hi Brajesh,

    Thank you so much for such a speedy reply.

    I appreciate that you have updated the code. I only have 5 roles I want to exclude and 1 role I want to include, so I think this piece of code will work for me, but I tried it and it didn’t work for me, it had this problem:

    When I tried the “Limiting to certain roles only.”, I added the one role I wanted to include in my directory and when I opened the directory, the page stays stuck on the “Loading the members of your community. Please wait.” message, and shows there are 6 members (when there should be 2 members, both with the same role I wanted to include). The same problem occured when I manually entered the 5 roles I want to exclude. I got the user roles from my wordpress dashboard.

    Also, I noted that you mentioned the other BuddyDev article with the code for excluding certain users, I used the code from the github page. Not sure if I was supposed to use the BuddyDev one?

    Thank you
    Stephanie

    • This reply was modified 1 month, 3 weeks ago by Stephanie. Reason: Add detail
  • Keymaster
    (BuddyDev Team)
    Posts: 24332
    Brajesh Singh on #52578

    Hi Stephanie,
    Thank you for the reply. I sincerely thank you for bringing that gist back to my knowledge. I had forgotten about that.

    1. Please share the complete code from bpdev_get_excluded_user_ids. There are 2 possible reasons:-
    a. The code uncommenting did not work as expected.
    b. or you have too many users and the query is stuck.

    How many users do you have on your site(just a ballpark idea is good enough).

    2. The code on github and BuddyDev do the same thing. They are alternatives.

    Thank you
    Brajesh

  • Participant
    Level: Enlightened
    Posts: 23
    Stephanie on #52603

    Hello Brajesh,
    Your last message landed in my junk folder so I did not see it till now.

    I am making a job board and want to hide the administrator (ie me) and the job posters so only job seekers are visible in the directory. I have 5 roles in total that I want to hide from the diirectory, including “administrator”, “job_poster”, “student” and two others.

    I do not know what their field IDs are and if that is needed but I got the role name from the Members plugin which lists all the user roles on my site.

    Also I do not have friends capability added on my site but I left that section of the code in because I am not sure if I can remove it.

    To answer your question, here is the exact code I used from the gist page:
    Attempt 1, trying to hide multiple roles, specifically “administrator” and “job_poster”:

    
    
    /**
     * Excludes users from BuddyPress Members list.
     *
     * @param string $qs Query string.
     * @param string $object object loop identikfier.
     *
     * @return string
     */
    function bpdev_exclude_users( $qs = false, $object = false ) {
    
    	if ( $object != 'members' ) { // hide for members only.
    		return $qs;
    	}
    
    	// comma separated ids of users whom you want to exclude.
    	$excluded_users = join( 'administrator', bpdev_get_excluded_user_ids() );
    
    	$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;
    	}
    
    	if ( ! empty( $args['exclude'] ) ) {
    		$args['exclude'] = $args['exclude'] . 'administrator' . $excluded_users;
    	} else {
    		$args['exclude'] = $excluded_users;
    	}
    
    	$qs = build_query( $args );
    
    	return $qs;
    }
    
    add_action( 'bp_ajax_querystring', 'bpdev_exclude_users', 20, 2 );
    
    /**
     * Retrieves an array of user ids to exclude.
     *
     * NOTE:- It is not an efficient idea if you want to exclude thousands of users. There are better ways.
     *
     * @return array
     */
    function bpdev_get_excluded_user_ids() {
    	/*
    	// Example:- Multiple roles Exclusion
    	$query = array(
    		'role__in' => array( 'administrator, job_poster' ),  // List of roles to exclude.
    		'fields'   => 'ID'
    	);
       */
    
    	return get_users( $query );
    }
    
    

    Attempt 2, Limiting display in directory to certain roles only by specifying the single role I want shown (a role called “talent”:

    
    
    <?php
    /**
     * Excludes users from BuddyPress Members list.
     *
     * @param string $qs Query string.
     * @param string $object object loop identikfier.
     *
     * @return string
     */
    function bpdev_exclude_users( $qs = false, $object = false ) {
    
    	if ( $object != 'members' ) { // hide for members only.
    		return $qs;
    	}
    
    	// comma separated ids of users whom you want to exclude.
    	$excluded_users = join( ',', bpdev_get_excluded_user_ids() );
    
    	$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;
    	}
    
    	if ( ! empty( $args['exclude'] ) ) {
    		$args['exclude'] = $args['exclude'] . ',' . $excluded_users;
    	} else {
    		$args['exclude'] = $excluded_users;
    	}
    
    	$qs = build_query( $args );
    
    	return $qs;
    }
    
    add_action( 'bp_ajax_querystring', 'bpdev_exclude_users', 20, 2 );
    
    /**
     * Retrieves an array of user ids to exclude.
     *
     * NOTE:- It is not an efficient idea if you want to exclude thousands of users. There are better ways.
     *
     * @return array
     */
    function bpdev_get_excluded_user_ids() {
    	
    	/*
    	// Example: Limiting to certain roles only.
    	$query = array(
    		'role__not_in' => array( 'talent' ),  // List of roles to include.
    		'fields'   => 'ID'
    	);
    	*/
    
    	return get_users( $query );
    }
    
    

    I am not a developer. There is a line that says $excluded_users = join( ',', bpdev_get_excluded_user_ids() );. Was I supposed to put the roles I want excluded from the directory between the ” in join(','?

    • This reply was modified 1 month, 3 weeks ago by Stephanie. Reason: Add detail
    • This reply was modified 1 month, 3 weeks ago by Stephanie. Reason: Add more detail
  • Participant
    Level: Enlightened
    Posts: 23
    Stephanie on #52633

    Hello,

    Just bumping this thread up so it is not overlooked and forgotten.

  • Participant
    Level: Enlightened
    Posts: 23
    Stephanie on #52704

    I have found and tested the suggestions on this page https://buddydev.com/hiding-users-on-buddypress-based-site/ and it worked for me so I will mark this as resolved now.

    For those who want to know how I did it: I used the main code for hiding users by user role written by Brajesh, then made the changes that user “Mitch” suggested and put it in the bp-custom.php file (Make a new text file and call it “bp-custom.php”, paste the code in, add the roles you want to exclude, and upload this to the hosting back end of your site to “wp-content/plugins”. I had about 5 user roles to exclude, so not a lot. If you want to exclude a lot, then I don’t know how to do it.

    Thank you

  • Keymaster
    (BuddyDev Team)
    Posts: 24332
    Brajesh Singh on #52730

    Thank you.
    I am glad you have resolved it.

    Regards
    Brajesh

You must be logged in to reply to this topic.

This topic is: resolved