Change default activity stream tab menu in Buddypress

It could seem very easy to change te default tab in the activity stream navigation menu, as in the buddypress/activity/index.php you can find all the elements of this menu, where the ‘all’ items has the ‘selected’ class.

I wanted to put the My groups tabs as the default one, so what I did was changing the <li> order in index.php of my buddypress child theme en then assign the selected class to the ‘My groups’ one instead of the default ‘All’, but it did not work.

It take me some time to figure out why it was not working, but at the end I could solve the issue.

The ‘selected’ class for tabs is used basically for css, but not for the query.
The query use cookies. There is a cookie named ‘bp-activity-scope’ that is used to query the posts.

So, to display the My groups posts this cookie must be set to ‘groups’ instead of ‘all’ which is the default one.
So I addedd a function in functions.php of my child theme to set the cookie to ‘groups’ and to run the query once again. The new query is executed each time that the reloads the activity stream page, so we are avoiding the default buddypress behaviour that makes the browser remember the last selected tab.


//Call the script that force the group tab to be the default one
function force_group_tab_in_activity_stream() {
//check if user is loggen in and if is in some group
if ( is_user_logged_in() && bp_get_total_group_count_for_user( bp_loggedin_user_id() ) ){
wp_register_script('hidelostpsw',get_stylesheet_directory_uri() . '/js/set_right_cookie.js' );
wp_enqueue_script('hidelostpsw');
}
}
add_action( 'wp_enqueue_scripts', 'force_group_tab_in_activity_stream' );

This is the content of the set_right_cookie.js file, located in this case in a js folder inside our child theme

var jq = jQuery;
jq(document).ready( function() {

filter = jq('#activity-filter-select select').val();
jq.removeCookie('bp-activity-scope', {
path: '/'
});
jq.cookie( 'bp-activity-scope', 'groups');
scope = jq.cookie('bp-activity-scope');
bp_activity_request(scope, filter);

} );

Now the My Groups tab will be the default selected tab, each time a user visit the page.

In addition, there is another problem.
When the user starts writing in the what’s new textares (the focus event) then the tab is switches again to ‘All’ .
Buddypress developers did so to avoid inconsistencies with the heartbeat integration.
It means that if the user has the Favorite tabs, or the group tabs selected, when he submit the post, it will probably appear in the wrong place….so the force te activity-all tab to be the selected one before postin.

But this is not a good solution if we want the groups to be more relevants than the general activity stream.

Si I found a solution that does the following:

If a user is going to post into a group, by selecting one from the whats-new-post-in drop down list, the tab will switch to ‘My Ggroups’ in case another one was selected.

If a user is going to post into My Profile, the activity-all tab will be selected.

In the example I am using the Buddy Wall plugin, which allows to define privacy setting.
But I’m not using privacy setting for groups (in fact they don’t even work :-( ), so an alert will be print during some second, notifyin users that Privacy settings are not available for groups.

For tat I first copied the file
wp-content/plugins/buddypress/bp-templates/bp-legacy/js/buddypress.js into my child theme directory inside the js/ folder (the same one where the previuos script was placed), in order to be able to edit it without loosing the code after upgrading.

Once we have the buddypress.js file in the right folder we will edit it.
We’ll locate the code inside the $whats_new.focus function on line 75

// Return to the 'All Members' tab and 'Everything' filter,
// to avoid inconsistencies with the heartbeat integration
if ( $activity_all.length ) {
if ( ! $activity_all.hasClass( 'selected' ) ) {
// reset to everything
jq( '#activity-filter-select select' ).val( '-1' );
$activity_all.children( 'a' ).trigger( 'click' );
} else if ( '-1' !== jq( '#activity-filter-select select' ).val() ) {
jq( '#activity-filter-select select' ).val( '-1' );
jq( '#activity-filter-select select' ).trigger( 'change' );
}
}

And we’ll replace it with the following

// Return to the 'All Members' tab and 'Everything' filter,
// to avoid inconsistencies with the heartbeat integration
if ( $activity_all.length ) {
var $activity_groups = jq( '#activity-groups' ),
$where_to_post= jq( '#whats-new-post-in-box select' ).val();
//If Post in is set to 'My Profile', switch selected tab to My Groups
if( $where_to_post !=='0'){
$activity_groups.children( 'a' ).trigger( 'click' );
// Set to 'public' and print message for the buddybos wall privacy settings
var $activity_visibility = jq( '#activity-visibility select' ).val( );
if ($activity_visibility !== 'public'){
jq( '#activity-visibility select' ).val( 'public' );
jq( '#activity-filter-select select' ).trigger( 'change' );
jq('

Privacy settings not available for groups

').appendTo('div#whats-new-submit').fadeIn('slow',
function(){
var el = jq(this);
setTimeout(function(){
el.fadeOut('slow',
function(){
jq(this).remove();
});
}, 4500);
});

}else{
jq('p.error').remove();
}
} else if ( ! $activity_all.hasClass( 'selected' ) || $where_to_post === '0') {

// reset to everything
jq('p.error').remove();
jq( '#activity-filter-select select' ).val( '-1' );
$activity_all.children( 'a' ).trigger( 'click' );
} else if ( '-1' !== jq( '#activity-filter-select select' ).val() ) {
jq( '#activity-filter-select select' ).val( '-1' );
jq( '#activity-filter-select select' ).trigger( 'change' );
}
}

Los comentarios están cerrados.