Default to My Groups instead of All Groups in BP Default theme

I am running a buddypress site consisting of hidden groups only. When a user clicks on the “Groups” tab in the main menu, it goes to the groups page which displays “No Groups found.” This confuses the members as they have to click the “My Groups” tab at the top to see their groups. Any way to default to the My Groups tab as active? Can't quite work it out as the tab logic is all driven by AJAX.

  • Mason

    Hiya Pete T,

    There's a simple way to do this, but you'll have to edit the theme template files. If you're not already doing so, you should create a child theme of BuddyPress default or your current theme so that these changes won't be overwritten.

    These instructions will work on the default theme (or child theme of default):
    navigate to bp-default/groups/index.php

    Open and go to line 37.

    Remove the class="selected" from the line so that it looks like so:
    <li id="groups-all"><a href="...

    Now move down to line 41 and add the selected class to this item. Making it look like this:
    <li class="selected" id="groups-personal"><a href="...

    Now the javascript will show the "my groups" tab by default when hitting the Groups slug in BuddyPress. Hope that helps!

    Thanks!

  • Pete T

    Thanks for the reply!

    Sadly, I already tried this to no avail. It does make the proper tabs active, but affects only the styles. Since the content for the tabs is generated via AJAX, it simply highlights the My Groups tab but still shows the no content message from All Groups. You have to actually click on "My Groups" to populate the list.

    To add, it also not as simple as manipulating the URL from the My Groups tab (via a custom menu item or such). That url is https://mydomain.com/members/pete/groups/my-groups/. If linked to directly it will display the "My Groups" tab in my profile page, not the groups list from the groups page. So BP is calling the div container contents and populating them somehow on the groups page.

    I am so incredibly disappointed with the buddypress developers for making the default theme so unbelievably complicated and difficult to edit. If I had it to do over again I would have chosen another theme on which to base my child theme. Now that I am some 200+ hours into the project I don't have the luxury of turning back. This is so much more difficult that it should be.

  • Pete T

    Looks like the key is modifying the AJAX query string, similar to here: https://premium.wpmudev.org/forums/topic/change-number-of-activities-shown-per-page

    By making a function like so:

    function \my_query_filter_new ( $query_string ) {
    	$query_string .= '&scope=personal&per_page=50';
    	return $query_string;
    }
    add_filter( 'bp_ajax_querystring', 'my_query_filter_new', 100);

    and forcing the scope to personal, it now at least starts with the "My Groups" tab and then quickly swtches to "All Groups," probably after reading the cookie. if you look at /_inc/ajax.php you can see the madness. I wish there was a way to turn off the AJAX functionality altogether.

    Also, would be nice to make this function fire only when "Groups" are being viewed instead of globally. Not sure how something like this will affect activities, etc. For now though, I do not see a solution without hacking the crap out of the core.

  • Pete T

    Think I got it FINALLY and without hacking the core. I'll post it here so no-one else need go through the absolute annoyance I have.

    Create a function in your buddypress child theme's functions.php:

    function my_query_filter_new ( $query_string ) {
    	global $bp;
    
    	// Create var array out of the query_string
    	parse_str($query_string, $str);
    
    	// Set MY GROUPS default tab
    	if (bp_is_page('groups') && $str['scope'] != 'personal') {
    		$query_string .= '&scope=personal';
    	}
    	// Set user_id if not already set for MY GROUPS tab
    	if (bp_is_page('groups') && $str['user_id'] == FALSE) {
    		$query_string .= '&user_id='.$bp->loggedin_user->id;
    	}
    	return $query_string;
    }
    add_filter( 'bp_ajax_querystring', 'my_query_filter_new', 100);

    Next, pull the \groups\index.php into your buddypress child theme and add/modify the tab section:

    <div class="item-list-tabs" role="navigation">
       <ul>
       <?php if ( is_user_logged_in() && current_user_can('manage_options') ) { ?>
       <li id="groups-all"><a href="<?php echo trailingslashit( bp_get_root_domain() . '/' . bp_get_groups_root_slug() ); ?>"><?php printf( __( 'All Groups <span>%s</span>', 'buddypress' ), bp_get_total_group_count() ); ?></a></li>
       <?php } ?>
    
       <?php if ( is_user_logged_in() && bp_get_total_group_count_for_user( bp_loggedin_user_id() ) ) : ?>
       <li <?php if (!current_user_can('manage_options')) { echo 'class="selected" '; } ?> id="groups-personal"><a href="<?php echo trailingslashit( bp_loggedin_user_domain() . bp_get_groups_slug() . '/my-groups' ); ?>"><?php printf( __( 'My Groups <span>%s</span>', 'buddypress' ), bp_get_total_group_count_for_user( bp_loggedin_user_id() ) ); ?></a></li>
       <?php endif; ?>
    
       <?php do_action( 'bp_groups_directory_group_filter' ); ?>
       </ul>
    </div><!-- .item-list-tabs -->

    This will show site admins both tabs (all groups and my groups) and show only the my groups tab for non-admins. I now have this seemingly working on my site. I'll post back if I expose any issues. Possible issues could be cookie related of how that ajax.php stores current states, etc. We'll see.

  • ice9design

    Great info!

    Question:
    You say "pull the \groups\index.php into your buddypress child theme and add/modify the tab section"

    I can't find \groups\index.php

    Where is it?

    And when I "pull it into" my child theme, do I make a folder called "groups" in the main section of my child and upload "index.php" into it?

    Sorry, I'm a bit confused (and I'm somewhat new to child themes as well).

  • Pete T

    Hey Ice,

    The BP theme files are in the buddypress plugin at:
    /wp-content/plugins/buddypress/bp-themes/bp-default

    Make a subfolder in your child theme called /buddypress. You can then place any of the folder/files from /bp-default to the /buddypress folder in your child theme. Any files in your child theme will override the default Buddypress ones.

    I like to add only ones I've modified to my child theme to avoid confusion.

    Here is some info, although I cant find the fantastic article I've used as a reference that lists each file and their override order.
    http://codex.buddypress.org/themes/building-a-buddypress-child-theme/

    So in your case, you would add the following:
    /buddypress/groups/index.php

    Hope this helps!

  • Jack Kitterhing

    Hi there @ice9design,

    Hope you're well today, you might want to look into filtering the groups and using a custom group loop, to return the current groups of the member.

    Part of the relevant code from BuddyPress is,

    /*** Group Fetching, Filtering & Searching  *************************************/
    
    /**
     * Get a collection of groups, based on the parameters passed
     *
     * @uses apply_filters_ref_array() Filter 'groups_get_groups' to modify return value
     * @uses BP_Groups_Group::get()
     * @param array $args See inline documentation for details
     * @return array
     */
    function groups_get_groups( $args = '' ) {
    
    	$defaults = array(
    		'type'              => false,    // active, newest, alphabetical, random, popular, most-forum-topics or most-forum-posts
    		'order'             => 'DESC',   // 'ASC' or 'DESC'
    		'orderby'           => 'date_created', // date_created, last_activity, total_member_count, name, random
    		'user_id'           => false,    // Pass a user_id to limit to only groups that this user is a member of
    		'include'           => false,    // Only include these specific groups (group_ids)
    		'exclude'           => false,    // Do not include these specific groups (group_ids)
    		'search_terms'      => false,    // Limit to groups that match these search terms
    		'meta_query'        => false,    // Filter by groupmeta. See WP_Meta_Query for syntax
    		'show_hidden'       => false,    // Show hidden groups to non-admins
    		'per_page'          => 20,       // The number of results to return per page
    		'page'              => 1,        // The page to return if limiting per page
    		'populate_extras'   => true,     // Fetch meta such as is_banned and is_member
    		'update_meta_cache' => true,   // Pre-fetch groupmeta for queried groups
    	);
    
    	$r = wp_parse_args( $args, $defaults );
    
    	$groups = BP_Groups_Group::get( array(
    		'type'              => $r['type'],
    		'user_id'           => $r['user_id'],
    		'include'           => $r['include'],
    		'exclude'           => $r['exclude'],
    		'search_terms'      => $r['search_terms'],
    		'meta_query'        => $r['meta_query'],
    		'show_hidden'       => $r['show_hidden'],
    		'per_page'          => $r['per_page'],
    		'page'              => $r['page'],
    		'populate_extras'   => $r['populate_extras'],
    		'update_meta_cache' => $r['update_meta_cache'],
    		'order'             => $r['order'],
    		'orderby'           => $r['orderby'],
    	) );
    
    	return apply_filters_ref_array( 'groups_get_groups', array( &$groups, &$r ) );
    }

    You can filter that as needed and return the required groups in the order needed. :slight_smile:

    Thanks!

    Kind Regards
    Jack.

  • ice9design

    Hi guys,
    First of all, Jack, I'm not sure what any of what you typed means. I'm sorry. Where is that code? How would I filter it?

    Secondly, something really weird happened. The above code is no longer working. When I go into "Groups," it's now defaulting to "All Groups" again. Instead of "My Groups."

    I have NO idea why. I even went over the above steps, again, and edited functions.php and index.php. No dice. Any thoughts on why it stopped working?

  • Jack Kitterhing

    Hiya @ice9design,

    Hope you're well today! :slight_smile:

    Sorry about the delay on this, just to clear up here what we need to do, a user should see all the groups they belong to but not other groups, this should also include the hidden groups which by default aren't included, correct? :slight_smile:

    What was the last thing that changed, before the code stopped working for you? Perhaps a theme or plugin update.

    Please advise and we'll get all the correct code you need to add. :slight_smile:

    Thanks!

    Kind Regards
    Jack.

  • Michelle Shull

    ice9! My forum friend!

    Oh no! This is a big mess, isn't it? I've got some ideas, after going through the recent threads where we've worked together, to get an idea about your update/changes timeline.

    The fact that it's working in FF is a good sign! Safari is picky, we can work around Safari, though. Have you tested in Chrome/IE? Any luck there?

    Let me know what kind of fruit the other browsers provide, and we'll move on from there.

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.