This topic has 10 replies, 3 voices, and was last updated 6 years by Kieran_SQ.

  • Author
  • #207503
     lusien
    Participant

    Hello,

    I would like to hide all users having admin role from being listed in the members directory and I couldn’t find a way this to work. I found different posts on the forum but none worked for me.

    Now I have this one being added in functions.php of my child theme but it does not work (1, 26 and 27 are the IDs of the users having Admin right):

    add_action(‘bp_ajax_querystring’,’bpdev_exclude_users’,20,2);
    function bpdev_exclude_users($qs=false,$object=false){
    //list of users to exclude

    $excluded_user=’1,26,27′;//comma separated ids of users whom you want to exclude

    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’])||!empty($args[‘search_terms’]))
    return $qs;

    if(!empty($args[‘exclude’]))
    $args[‘exclude’]=$args[‘exclude’].’,’.$excluded_user;
    else
    $args[‘exclude’]=$excluded_user;

    $qs=build_query($args);

    return $qs;

    }

     

    Can you please help?

     

     

    #207509
     Kieran_SQ
    Moderator

    Hi,

    The code you shared without the tags has lost it’s formatting so I cannot use it as an example, to share code here in the future please use a code tag (from the reply area) before and after the code.

    If the code from our support article is not working for you then you may wish to try this from the BuddyDev team https://buddydev.com/exclude-users-from-members-directory-on-a-buddypress-based-social-network/.

    Thanks,

    Kieran

    Hi there!!! Help others from the community and mark any reply as solution if it solved your question. Mark as a solution

    If you like the theme or the support you've received please consider leaving us a review on Themeforest!

    Custom development requests can be sent to dev@seventhqueen.com, one of the development team will be happy to discuss your needs.

    #207600
     lusien
    Participant

    Hi Kieran,

    Thanks for trying to help. It is what I have added, both (from KB and BuddyDEv) and it does not work in my case. Can you please check if I am doing something wrong in my functions.php file (lines 50 – 76) from my child-theme? Credentials have been provided.

    Thanks.

    #207660
     Kieran_SQ
    Moderator

    Hi,

    I did some testing on your site and away from your site and found the above code is working, as well as the code below, on my local and live environment but not on your site. I think you’ve either got custom code or a third party plugin that is interfering with the code running.

    You’ll need to test this yourself in a staging environment and disable plugins/custom code until you find what is interacting with it.

    COPY CODE
    
    // Exclude administrator role from loop
    
    function bgmc_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 bgmc_exclude_by_role( $exclude_roles, $implode = true ) {
        $memberArray = array();
        foreach ( $exclude_roles as $exclude_role ) $memberArray = array_merge( $memberArray, bgmc_get_user_ids_by_role( $exclude_role ) );
       
        if ( !$implode ) return $memberArray;
       
        $theExcludeString = implode( ",", $memberArray );
       
        return $theExcludeString;
    }
     
    function bgmc_exclude_byroles( $qs=false,$object=false ) {
     
        if ( $object!='members' ) return $qs; //hide for members only
     
        $args = wp_parse_args( $qs );
       
        if ( !empty( $args['user_id'] ) ) return $qs; //check if we are searching for friends list etc?, do not exclude in this case
     
        $excluded_roles = array( 'administrator' ); // you can add roles here
     
        $exclude = get_transient( 'excluded_members' ); // Use a transient to prevent overloading the database on sites with a lot of members
       
        if ( !$exclude ) :
            $exclude = bgmc_exclude_by_role( $excluded_roles );
            set_transient( 'excluded_members', $exclude, WEEK_IN_SECONDS );
        endif;
     
        if ( !empty( $args['exclude'] ) ):
            $args['exclude'] = $args['exclude'].','.$exclude;
        else:
            $args['exclude'] = $exclude;
        endif;
         
        $qs = build_query($args);
       
        return $qs;
    }
    add_action( 'bp_ajax_querystring', 'bgmc_exclude_byroles', 20, 2 );

    Thanks,

    Kieran

    Hi there!!! Help others from the community and mark any reply as solution if it solved your question. Mark as a solution

    If you like the theme or the support you've received please consider leaving us a review on Themeforest!

    Custom development requests can be sent to dev@seventhqueen.com, one of the development team will be happy to discuss your needs.

    #207670
     lusien
    Participant

    Hi Kieran,

    Thanks for your support. Indeed, you were right. I found out that BP registration options plugin is causing the issue. However, I need this plugin because this way I can approve newly created users before they are able to enter the community.

    I will open a ticket with them to see if they can further help.

    Regards.

    #207691
     Kieran_SQ
    Moderator

    Hi,

    Glad you found the conflicting plugin. If the reply you get from the plugin developer provides a solution to the issue please do let me know.

    All the best,

    Kieran

    Hi there!!! Help others from the community and mark any reply as solution if it solved your question. Mark as a solution

    If you like the theme or the support you've received please consider leaving us a review on Themeforest!

    Custom development requests can be sent to dev@seventhqueen.com, one of the development team will be happy to discuss your needs.

    #207924
     lusien
    Participant

    Hi Kieran,

    Just an update. I have been intrigued by the problem so I continued my investigation and let me share with you what I have found out. It is a certain setting in BP Registration Options pluggin which actually generates the issue. As soon as I checked the option “Only registered or approved members can view BuddyPress/bbPress pages (Private Network)” hiding the users according to the script in Members’ Directory is not working anymore. If this option is not checked, users are correctly hidden.

    The developer is looking into this right now. I’ll keep you informed.

    Regards.

    #207935
     Kieran_SQ
    Moderator

    Hi,

    Thanks for keeping me updated – It’s a strange issue indeed. I hope you get a resolution for the issue.

    Kieran

    Hi there!!! Help others from the community and mark any reply as solution if it solved your question. Mark as a solution

    If you like the theme or the support you've received please consider leaving us a review on Themeforest!

    Custom development requests can be sent to dev@seventhqueen.com, one of the development team will be happy to discuss your needs.

    #208753
     lusien
    Participant

    Hi Kieran,

    I was in touch with the author of the BP-registration-options and he confirmed the issue. He fixed it for me and committed to add the solution in the new release of the plugin.

    Regards.

    #208863
     Radu
    Moderator

    Great
    Cheers
    R

    Hi there!!! Help others from the community and mark any reply as solution if it solved your question. Mark as a solution
    #208946
     Kieran_SQ
    Moderator

    Hi,

    Sorry for the late reply I was travelling and had no internet connection. That’s great to hear and I will make a note should the issue arise for any other users.

    Thanks so much for getting back to me.

    All the best,

    Kieran

    Hi there!!! Help others from the community and mark any reply as solution if it solved your question. Mark as a solution

    If you like the theme or the support you've received please consider leaving us a review on Themeforest!

    Custom development requests can be sent to dev@seventhqueen.com, one of the development team will be happy to discuss your needs.

Viewing 11 posts - 1 through 11 (of 11 total)

You must be logged in to reply to this topic.

Log in with your credentials

Forgot your details?