BuddyDev

Search

[Resolved] Display GROUPS Alphabetically in List

  • Participant
    Level: Enlightened
    Posts: 133
    Known-Admin on #48631

    Hello,

    I’m trying to display the Groups list by Group NAME alphabetically for the current user. I have the display part working with the links appearing, underlined. It’s looking great.

    However, I’ve tried several options to get my list in alphabetical order and have not been successful. Here is the code I’m using to display the Groups list (without ordering):

    
    
     
    function user_world_memberships(){
    	$group_ids = groups_get_user_groups(bp_displayed_user_id());
    	foreach($group_ids["groups"] as $group_id) { 
    		echo(
    		'<a title="View group page" href="' . home_url() . '/groups/' . groups_get_group(array( 'group_id' => $group_id )) -> slug . '"><u>' . 
    		groups_get_group(array( 'group_id' => $group_id )) -> name . '</u></a>' . 
    		(end($group_ids["groups"]) == $group_id ? '' : '<br> ' ) ); 
    			}
    }
    
    add_shortcode('member_worlds', 'user_world_memberships');
    
    

    I don’t know if you help with such things. I could sure use your assistance.

    I would also like to display a similar ordered list as described above, but listing only the GROUPS where the user is the GROUP ADMIN.

    A third is an ordered list of ALL GROUPS and the names of their GROUP ADMINS (also alphabetical). For example Group name #1: Admin #1, Admin #2, Group name #2: Admin #1, Group name #3: Admin #1, Admin #2, Admin #3, etc.

    Finally, a fourth list, the same as #3 above, but Lists ALL GROUP ADMINS (ordered by Admin) and a list of all their Groups (groups also ordered alphabetically).

    I’m attempting to put together an administrative page for the Site Admin and to display a list of all groups for a particular user on our NEWS page (the page is a normal page and it is not displayed within a group tab).

    Best Regards,
    NoNAdmin

    • This topic was modified 1 year, 1 month ago by Known-Admin.
  • Keymaster
    (BuddyDev Team)
    Posts: 24211
    Brajesh Singh on #48648

    Hi Dianne,
    Thank you for the question.

    I have modified your shcorcode for flexibility. I hope it helps.

    
    /**
     * Shortcode for listing groups of a user.
     *
     * @param array  $atts shortcode attributes.
     * @param string $content content.
     *
     * @return string
     */
    function user_world_memberships( $atts, $content = '' ) {
    
    	$atts = shortcode_atts(
    		array(
    			'user_id'            => bp_displayed_user_id(),
    			'type'               => 'alphabetical',
    			'order'              => 'ASC',
    			'page'               => 1,
    			'per_page'           => - 1,
    			'group_type'         => '',
    			'group_type__in'     => '',
    			'group_type__not_in' => '',
    		),
    		$atts,
    		'member_worlds'
    	);
    
    	$groups = groups_get_groups( $atts );
    
    	$groups = $groups['groups'];
    	if ( empty( $groups ) ) {
    		return '';
    	}
    
    	$list = '';
    	foreach ( $groups as $group ) {
    		$list .= sprintf( '<li><a title="View %1$s" href="%2$s">%3$s</a>', esc_attr( $group->name ), esc_url( bp_get_group_permalink( $group ) ), esc_html( $group->name ) );
    	}
    
    	if ( ! empty( $list ) ) {
    		$list = "<ul class='custom-user-group-list'>{$list}</ul>";
    	}
    
    	return $list;
    }
    
    add_shortcode( 'member_worlds', 'user_world_memberships' );
    
    

    P.S. we normally void providing coding support for 3rd party plugins but there are always exceptions.

    Regards
    Brajesh

  • Participant
    Level: Enlightened
    Posts: 133
    Known-Admin on #48666
    This reply has been marked as private.
  • Keymaster
    (BuddyDev Team)
    Posts: 24211
    Brajesh Singh on #48675
    This reply has been marked as private.
  • Participant
    Level: Enlightened
    Posts: 133
    Known-Admin on #48684
    This reply has been marked as private.
  • Keymaster
    (BuddyDev Team)
    Posts: 24211
    Brajesh Singh on #48698
    This reply has been marked as private.
  • Participant
    Level: Enlightened
    Posts: 133
    Known-Admin on #48779
    This reply has been marked as private.
  • Keymaster
    (BuddyDev Team)
    Posts: 24211
    Brajesh Singh on #48793
    This reply has been marked as private.
  • Keymaster
    (BuddyDev Team)
    Posts: 24211
    Brajesh Singh on #48801

    Hi Dianne,
    Thank you for your patience.

    Please remove the old code and use the following.

    
    
    /**
     * Returns a list of user links sorted alphabetically.
     *
     * @param array $group_users user ids.
     *
     * @return string
     */
    function bp_custom_get_alpha_sorted_user_links( $group_users ) {
    
    	if ( empty( $group_users ) ) {
    		return '';
    	}
    
    	$group_user_ids = wp_list_pluck( $group_users, 'user_id' );
    
    	$users = get_users(
    		array(
    			'include' => $group_user_ids,
    			'orderby' => 'display_name',
    			'order'   => 'ASC',
    		)
    	);
    
    	$markup = '';
    
    	foreach ( $users as $user ) {
    		$markup .= sprintf( '<a href="%1$s" class="custom-user-group-user-link">%2$s</a>', esc_url( bp_core_get_user_domain( $user->ID ) ), bp_core_get_user_displayname( $user->ID ) );
    	}
    
    	return $markup;
    }
    
    function user_world_memberships( $atts, $content = '' ) {
    
    	$atts = shortcode_atts(
    		array(
    			'user_id'            => bp_displayed_user_id(),
    			'type'               => 'alphabetical',
    			'order'              => 'ASC',
    			'page'               => 1,
    			'per_page'           => - 1,
    			'group_type'         => '',
    			'group_type__in'     => '',
    			'group_type__not_in' => '',
    			'group_role'         => '', // 'admin' , 'mod', 'member'.
    			'list_admins'        => 0,
    		),
    		$atts,
    		'member_worlds'
    	);
    
    	$list_admins = (bool) $atts['list_admins'];
    	$user_groups = array();
    
    	if ( 'admin' === $atts['group_role'] ) {
    		$user_groups = bp_get_user_groups(
    			$atts['user_id'],
    			array(
    				'is_admin' => true,
    				'per_page' => $atts['per_page'],
    				'page'     => $atts['page'],
    			)
    		);
    	} elseif ( 'mod' === $atts['group_role'] ) {
    		$user_groups = bp_get_user_groups(
    			$atts['user_id'],
    			array(
    				'is_mod'   => true,
    				'per_page' => $atts['per_page'],
    				'page'     => $atts['page'],
    			)
    		);
    	}
    
    	if ( in_array( $atts['group_role'], array( 'admin', 'mod' ), true ) ) {
    		if ( empty( $user_groups ) ) {
    			return '';
    		}
    
    		$group_ids       = array_keys( $user_groups );
    		$atts['include'] = $group_ids;
    		unset( $atts['user_id'] );
    	}
    
    	$groups = groups_get_groups( $atts );
    
    	$groups = $groups['groups'];
    	if ( empty( $groups ) ) {
    		return '';
    	}
    
    	$list = '';
    	foreach ( $groups as $group ) {
    		$admin_markup = '';
    		if ( $list_admins ) {
    			$admin_markup = bp_custom_get_alpha_sorted_user_links( groups_get_group_admins( $group->id ) );
    		}
    		$list .= sprintf( '<li><a title="View %1$s" href="%2$s" class="custom-user-group-group-link">%3$s</a>%4$s</li>', esc_attr( $group->name ), esc_url( bp_get_group_permalink( $group ) ), esc_html( $group->name ), $admin_markup );
    	}
    
    	if ( ! empty( $list ) ) {
    		$list = "<ul class='custom-user-group-list'>{$list}</ul>";
    	}
    
    	return $list;
    }
    
    add_shortcode('member_worlds', 'user_world_memberships');
    

    You can pass group_role and list_admins options to list specific groups as in your initial requirement. It should handle most of the earlier use case.

    Regards
    Brajesh

You must be logged in to reply to this topic.

This topic is: resolved