BuddyDev

Search

Hide group roles based on group type

Tagged: 

  • Participant
    Level: Initiated
    Posts: 11
    Omar on #40214

    Hey! Thank you for all the help you guys do for us here.

    I’m trying to hide a specific group role in the groups member tab, and I’m trying to do that based on the group type. Is that possible?

    Thanks a million!

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 3115
    Ravi on #40236

    Hello Omar,

    Thank you for posting. By group role, I am assuming you are talking about Group Members, Group Moderators, and Group Admins. On the basics of group type, you want to hide them. Is it you are looking for?

    Regards
    Ravi

  • Participant
    Level: Initiated
    Posts: 11
    Omar on #40241

    Correct, for example:

    Hide only moderators for group type A
    Hide only admins for group type B

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 3115
    Ravi on #40249

    Hello Omar,

    Thank you for the acknowledgment. Please try the following code:

    
    
    add_filter( 'bp_after_group_has_members_parse_args', function ( $args ) {
    	$group = groups_get_current_group();
    
    	$types = bp_groups_get_group_type( $group->id, false );
    
    	if ( ! $types ) {
    		return $args;
    	}
    
    	$exclude = array();
    
    	if ( in_array( 'A', $types ) && bp_group_has_moderators( $group ) ) {
    		// Exclude moderators.
    		$exclude = bp_group_mod_ids( $group, 'array' );
    	} elseif ( in_array( 'B', $types ) ) {
    		// Exclude Administrators.
    		$exclude = bp_group_admin_ids( $group, 'array' );
    	}
    
    	$args['exclude'] = empty( $args['exclude'] ) ? $exclude : array_merge( (array) $args['exclude'], $exclude );
    
    	return $args;
    } );
    
    

    Let me know if it helps or not.

    Regards
    Ravi

  • Participant
    Level: Initiated
    Posts: 11
    Omar on #40276

    It is helping. I’m trying to hide two roles but is not working for me, not sure what I’m doing wrong. That’s why the delayed response, I was trying to do it myself before asking. Can you help me with that too?

  • Participant
    Level: Initiated
    Posts: 11
    Omar on #40278

    I mean, I would like to hide two roles per group type

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 3115
    Ravi on #40300

    Hello Omar,

    Try the following solution:

    
    /**
     * Get group moderator ids.
     *
     * @param BP_Groups_Group $group Group object.
     *
     * @return array
     */
    function buddydev_get_group_member_ids( $group ) {
    	global $wpdb;
    
    	$table = buddypress()->groups->table_name_members;
    
    	$query = $wpdb->prepare( "SELECT user_id FROM {$table} WHERE group_id = %d AND is_admin = 0 AND is_mod = 0 AND is_confirmed = 1", $group->id );
    
    	return $wpdb->get_col( $query );
    }
    
    add_filter( 'bp_after_group_has_members_parse_args', function ( $args ) {
    	$group = groups_get_current_group();
    
    	$types = bp_groups_get_group_type( $group->id, false );
    
    	if ( ! $types ) {
    		return $args;
    	}
    
    	$admin_ids   = bp_group_admin_ids( $group, 'array' );
    	$mod_ids     = bp_group_has_moderators( $group ) ? bp_group_mod_ids( $group, 'array' ) : array();
    	$members_ids = buddydev_get_group_member_ids( $group );
    
    	$exclude = array();
    
    	if ( in_array( 'A', $types ) ) {
    		$exclude = array_merge( $admin_ids, $mod_ids );
    	} elseif ( in_array( 'B', $types ) ) {
    		$exclude = array_merge( $admin_ids, $members_ids );
    	}
    
    	$args['exclude'] = empty( $args['exclude'] ) ? $exclude : array_merge( (array) $args['exclude'], $exclude );
    
    	return $args;
    } );
    
    

    Regards
    Ravi

  • Participant
    Level: Initiated
    Posts: 11
    Omar on #40330

    TY very much Ravi, it’s working perfectly. One more thing if you can, What should I do if I want to actually show all member roles for a specific group type? Cause now I notice that if I don’t add the group type into the code, that group type won’t show any member roles.

    Thanks a lot man! You’re the best!

  • Keymaster
    Level: Yogi
    (BuddyDev Team)
    Posts: 3115
    Ravi on #40338

    Hello Omar,

    Thank you for the acknowledgment.

    Cause now I notice that if I don’t add the group type into the code, that group type won’t show any member roles.

    Please post your code for this.

    What should I do if I want to actually show all member roles for a specific group type?

    
    
    if ( in_array( 'A', $types ) ) {
    		$exclude = array_merge( $admin_ids, $mod_ids );
    	} elseif ( in_array( 'B', $types ) ) {
    		$exclude = array_merge( $admin_ids, $members_ids );
    	}
    
    // Reset excluded array if group of specfic type.
    if ( in_array( 'C', $types ) ) {
    		$exclude = array();
    	}
    
    

    Regards
    Ravi

You must be logged in to reply to this topic.

This topic is: not resolved