Re: BP 1.2: How to add a custom tab on the Home activity section
so here’s a tweaked version and the latest in my custom tab efforts, fyi:
// passes MyClass scope into ajax query for custom Class Year activity filter
function bn_ajax_querystring_activity_filter( $query_string, $object, $filter, $scope, $page, $search_terms, $extras ) {
global $bp;
if ( !is_user_logged_in() || $object != 'activity' )
return $query_string;
$args = array();
parse_str( $query_string, $args );
$args['object'] = $bp->groups->id;
switch ($scope){
case 'myclass':
$slug = "usma-". bp_get_profile_field_data( array('field'=> 'Class Year', 'user_id' => $bp->loggedin_user->id));
$groupid = BP_Groups_Group::get_id_from_slug($slug);
$args['primary_id'] = $groupid;
break;
case 'mycadetco':
$slug = bp_get_profile_field_data( array('field'=> 'Cadet Company', 'user_id' => $bp->loggedin_user->id));
$groupid = BP_Groups_Group::get_id_from_slug($slug);
$args['primary_id'] = $groupid;
break;
case 'mybranch':
$slug = sanitize_title(bp_get_profile_field_data( array('field'=> 'Branch', 'user_id' => $bp->loggedin_user->id)));
$groupid = BP_Groups_Group::get_id_from_slug($slug);
$args['primary_id'] = $groupid;
break;
default:
return $query_string;
}
return http_build_query( $args );
}
add_filter( 'bp_dtheme_ajax_querystring', 'bn_ajax_querystring_activity_filter', 1, 7 );
and the action:
function bn_add_activity_tab() {
if ( !is_user_logged_in() )
return false;
?>
<!-- default item-list-tabs activity-type-tabs
<ul> -->
</div><!-- default item-list-tabs activity-type-tabs -->
<div class="item-list-tabs activity-type-tabs" id="bn-tabs">
</ul>
<ul>
<li id="activity-myclass">
<a>" title="<?php _e( 'Activity for my Class Year', 'buddypress' ) ?>">
<?php printf( __( 'My Class', 'buddypress' ) ) ?>
</a>
<li id="activity-mycadetco">
<a>" title="<?php _e( 'Activity for my Cadet Company', 'buddypress' ) ?>">
<?php printf( __( 'My Cadet Company', 'buddypress' ) ) ?>
</a>
<li id="activity-mybranch">
<a>" title="<?php _e( 'Activity for my Army Branch', 'buddypress' ) ?>">
<?php printf( __( 'My Branch', 'buddypress' ) ) ?>
</a>
<?php
}
add_action( 'bp_activity_type_tabs', 'bn_add_activity_tab', 1, 2 );
A few things to note:
1. use the WP function sanitize_title() if you have a field that might return more than one word e.g. the group ‘Military Police’ returns ‘military police’ but transforms into ‘military-police’ (a pretty slug0 which gives you a correct slug now.
2. I added addl markup to force my new tabs into their own element.
3. Don’t be shy to make suggestions and keep improving this technique. I think many will use it to customize their installs.