BP 1.2: How to add a custom tab on the Home activity section
-
Props to @jjj and @Mrmaz for their help in this. This technique turned out to be much less trivial than we thought (at least in the hunt for a solution) but it adds a dynamic to your site that might prove very useful.
The Need: I will use my site as an example and for context. We run an alumni site and have Class groups created back to 1939. We also have groups that are not class specific (e.g. Reading Room, Politics, etc) The new activity stream in 1.2 will, no doubt, be a huge hit and boost engagement. The ‘My Groups’ tab is awesome to see stuff in all the groups you care about i.e. are a member of. In our case, members really care about all the activity in their Class.
Requirement: Add a tab that filters activity for a user’s Class – or said differently – for a specified Group.
The Solution:
Step 1: create a function that adds a filter to modify the query used in creating the activity stream. In the activity stream, a $scope needs to be specified. In this example ‘myclass’ is the scope. This function also checks to ensure a user is logged in. Finally,
function_to_get_group_id_for_logged_in_user()
is simply a placeholder for one of 2 things: you either hard code a Group ID of your choosing, or (as in my case) you add a function to dynamically insert a Group ID – we do a look up of a user’s Class field value). These functions go, of course, in your theme functions.php file (or even in bp-custom.php in the plugin folder)
function bp_my_ajax_querystring_activity_filter( $query_string, $scope ) {
global $bp;
if ( $scope != 'myclass' || !is_user_logged_in() )
return $query_string;
$args = array();
parse_str( $query_string, $args );
$args['object'] = $bp->groups->id;
$args['primary_id'] = function_to_get_group_id_for_logged_in_user();
return http_build_query( $args );
}
add_filter( 'bp_dtheme_ajax_querystring_activity_filter',
'bp_my_ajax_querystring_activity_filter', 1, 2 );Step 2: Create the tab list item
In this case, I am in a child theme off the parent bp-default. You need to create the folder /activity in your theme and copy /activity/index.php into that new folder. Then add the following line in the list class “item-list-tabs activity-type-tabs”
<li id="activity-myclass"><a href="<?php echo site_url( BP_ACTIVITY_SLUG . '/#myclass/' ) ?>" title="<?php _e( 'Activity for my Class Year.', 'buddypress' ) ?>"><?php printf( __( 'My Class', 'buddypress' ) ) ?></a></li>
Important!: notice the list-id has ‘myclass’ in the name. You must include your scope name here for the scope to be passed properly. The #myclass slug can actually be whatever you want as long as it’s unique (my suggestion is keep it the same or close for consistency) The rest of the customization is up to you (title, etc)
That’s it. Now, when user’s click that tab, they get all the functionality of the other tabs but for one particular group. I can see this having many uses, depending on the focus of your BP site.
@Andy – feel free to copy this as a How To in Docs if you deem it worthy (or not riddled with errors!)
- The topic ‘BP 1.2: How to add a custom tab on the Home activity section’ is closed to new replies.