Skip to:
Content
Pages
Categories
Search
Top
Bottom

[Resolved] Display groups a user is member of

  • @azinfiro

    Participant

    I’m trying to display the names of the groups a given user is a member of. Unfortunately, there is no function already doing this so I’m trying to put something together myself with my very limited knowledge of PHP.

    So far I have this: BP_Groups_Member::get_group_ids($user_id);

    This is supposed to get me the group ids which I still need to turn into group names.
    Can anyone help me with that?

    Thanks.

Viewing 5 replies - 1 through 5 (of 5 total)
  • @shanebp

    Moderator

    @azinfiro

    Participant

    Thank you for your reply.
    I did come across that thread earlier, but the answers didn’t seem to work for me.

    Anyway, I used the groups_get_user_groups() function you mentioned in your reply and came up with a solution doing what I was looking for along with comma separation of the group names:

    <?php 
    $group_ids = groups_get_user_groups(bp_displayed_user_id());
    foreach($group_ids["groups"] as $group_id) { 
    	echo(groups_get_group(array( 'group_id' => $group_id )) -> name . (end($group_ids["groups"]) == $group_id ? '' : ', ' ) ); 
    	}
    ?>

    @azinfiro

    Participant

    Just in case anybody else needs this too, I have included an updated version of the code in form of a function. It now displays links to the groups a user is a member of and it’s possible to toggle display of hidden groups.

    // display links to the groups a user is member of
    function user_group_memberships( $user_id = null, $show_hidden = false ) {
    
    	$group_ids = groups_get_user_groups($user_id);
    	
    	$visible_group_ids = array();
    	
    	foreach($group_ids["groups"] as $group_id) { 
    		if (!$show_hidden) {
    			if(groups_get_group(array( 'group_id' => $group_id )) -> status !== 'hidden') {
    			$visible_group_ids[] = $group_id;
    			}
    		} else {
    		$visible_group_ids[] = $group_id;
    		}
    	}
    	
    	if (empty($visible_group_ids)) {
    		echo 'None';
    	} else {
    		foreach($visible_group_ids as $visible_group_id) {
    			echo(
    				'<a title="View group page" href="' . home_url() . '/groups/' . groups_get_group(array( 'group_id' => $visible_group_id )) -> slug . '">' . 
    				groups_get_group(array( 'group_id' => $visible_group_id )) -> name . '</a>' . 
    				(end($visible_group_ids) == $visible_group_id ? '' : ', ' ) 
    			);
    		}
    	}
    }

    This is my first ever PHP function so any feedback is welcome.

    @dolliv

    Participant

    how did you implement that into (I guess) a page? Did you use a shortcode?

    @danbp

    Participant

    Hi @dolliv,

    custom functions are generally added to one of these files:

    functions.php


    https://codex.buddypress.org/plugindev/bp-custom-php/

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘[Resolved] Display groups a user is member of’ is closed to new replies.
Skip to toolbar