Support only during business hours : Monday to friday, from 8:30 am – 5:30 pm CEST

Due to the decrease in our staff due to vacations, our response time may be longer.

Be sure we're doing our best to manage your topic as soon as possible.

Extend code snippet to exclude role

This topic is resolved
Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • alex
    Participant
    • 81 Topics
    • 259 Posts
    @abourne

    Require extending this code to exclude the role ‘trainee’ in addition to admin.

    add_filter( 'bp_after_has_members_parse_args', 'buddydev_exclude_users_by_role' );
    
    function buddydev_exclude_users_by_role( $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 );
        }
    
        $role = 'administrator';//change to the role to be excluded
        $user_ids =  get_users( array( 'role' => $role ,'fields'=>'ID') );
    
        $excluded = array_merge( $excluded, $user_ids );
    
        $args['exclude'] = $excluded;
    
        return $args;
    }

    Tried extending this part to ‘trainee’ but only broke the site…

    Must be doing something wrong. Please help to make proper snippet to exclude ‘trainee’.

    $role = ‘administrator’;//change to the role to be excluded
    $user_ids = get_users( array( ‘role’ => $role ,’fields’=>’ID’) );

    Themosaurus
    Keymaster
    Themosaurus Support
    • 1 Topics
    • 1675 Posts
    @themosaurus

    Hi @abourne,

    It should work fine if you replace the 'administrator' value by 'author' or 'subscriber'. The value ‘trainee’ doesn’t exist by default and we’re not sure that it has been properly created or recognized by BuddyPress. Moreover, it seems that your snippet does not work anymore once the AJAX filter values are changed (for instance, from Last Active to Alphabetical). So, it seems this snippet won’t work across the board…

    Please note that this issue is related with the customization of BuddyPress, not with the use of your theme. As theme authors, the installation, setting or debugging of third party plugins isn’t part of the support we will provide.

    To go further, we invite you to ask for more help from the plugin authors and post your request on their support forums:
    https://buddypress.org/support/

    We remain available for all support requests related with the use of your theme.

    Regards,

    alex
    Participant
    • 81 Topics
    • 259 Posts
    @abourne

    Thanks for the comment.

    “pro” and “trainee” are roles I created.

    WP successfully recognises the roles.

    I did a test of the snippet using just “pro” and just “administrator”

    Both work fine.

    Just need to do in conjunction, as I cannot redeclare the function.

    Please share thoughts on snippet to extend for ‘two’ role exclusions.

    Thanks

    alex
    Participant
    • 81 Topics
    • 259 Posts
    @abourne

    Nevermind, I just figured it out. For you forums reference:

    add_filter( 'bp_after_has_members_parse_args', 'buddydev_exclude_users_by_role' );
    
    function buddydev_exclude_users_by_role( $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 );
        }
    
        //change 'role' to the role to be excluded
        $user_ids_customer 	=  get_users( array( 'role' => 'administrator' 	,'fields'=>'ID') );
        $user_ids_admin 	=  get_users( array( 'role' => 'trainee' 		,'fields'=>'ID') );
    
        $excluded = array_merge( $excluded, $user_ids_customer, $user_ids_admin );
    
        $args['exclude'] = $excluded;
    
        return $args;
    }
    Themosaurus
    Keymaster
    Themosaurus Support
    • 1 Topics
    • 1675 Posts
    @themosaurus

    Hi @abourne,

    Glad you were able solve your problem :).
    And thank you very much for your feedback on this one, it will probably help other users! ?

    Best regards,

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

The topic ‘Extend code snippet to exclude role’ is closed to new replies.

Troubleshooting Demo Imports

You're trying to setup your theme but you're experiencing errors when importing the demo content? Or you've just followed the setup guide but your website doesn't look exactly like our demo? These are common issues for which you can find easy and quick fixes.

Happy With our Support So Far?

Feel free to review our theme on Themeforest! It helps us making our products more known to new potential customers, which allow us more time to improve the quality and develop new features. #SharingIsCaring ❤️

Discover MatchPress

Skip • Like • Super-Like

Add powerful matching features like Member likes, skips, super likes, conditional private messaging and much more.

Setup Your Cera or Gwangi powered Community Website and Turn it into an iOS and Android App

15% discount for Cera users

To unleash the full power of your Cera or Gwangi theme, we have partnered with the Zipline team. Your community website can now be fully setup and turned into your very own custom app for iOS and Android.

Whether you've newly acquired the theme or already got your site up and running, Zipline got you covered. And we got you an incredible discount.

Holiday, Weather & Festive effects
to pimp your WordPress Site