Activity dropdown filters in templates

Activity dropdown filters are used in order to let a user filter the activity stream in three main contexts: the activity directory, the single member activity page and the single group activity page.

The Activity directory dropdown filters.

The Activity directory dropdown filters.


Version 2.1 of BuddyPress introduced the template tag bp_activity_show_filters() to dynamically generate the options of the activity dropdown filters. While the BP Theme compatibility templates are using this function, the BP-Default templates are not since Core Team has decided that no further development on the theme template files will be made. The goal of this article is to show you how you can enjoy this new template tag within your BP-Default child theme.

Use bp_activity_show_filters() within your BP-Default child theme Use bp_activity_show_filters() within your BP-Default child theme

The three templates that are containing an activity dropdown filters are:

  • Activity directory: activity/index.php
  • Single Member activity page: members/single/activity.php
  • Single Group activity page: groups/single/activity.php

Let’s look at the activity directory template, focusing on the select tag having the css id “activity-filter-by”:

[sourcecode language=”php”]
<select id="activity-filter-by">
<option value="-1"><?php _e( ‘&mdash; Everything &mdash;’, ‘buddypress’ ); ?></option>
<option value="activity_update"><?php _e( ‘Updates’, ‘buddypress’ ); ?></option>

<?php if ( bp_is_active( ‘blogs’ ) ) : ?>

<option value="new_blog_post"><?php _e( ‘Posts’, ‘buddypress’ ); ?></option>
<option value="new_blog_comment"><?php _e( ‘Comments’, ‘buddypress’ ); ?></option>

<?php endif; ?>

<?php if ( bp_is_active( ‘forums’ ) ) : ?>

<option value="new_forum_topic"><?php _e( ‘Forum Topics’, ‘buddypress’ ); ?></option>
<option value="new_forum_post"><?php _e( ‘Forum Replies’, ‘buddypress’ ); ?></option>

<?php endif; ?>

<?php if ( bp_is_active( ‘groups’ ) ) : ?>

<option value="created_group"><?php _e( ‘New Groups’, ‘buddypress’ ); ?></option>
<option value="joined_group"><?php _e( ‘Group Memberships’, ‘buddypress’ ); ?></option>

<?php endif; ?>

<?php if ( bp_is_active( ‘friends’ ) ) : ?>

<option value="friendship_accepted,friendship_created"><?php _e( ‘Friendships’, ‘buddypress’ ); ?></option>

<?php endif; ?>

<option value="new_member"><?php _e( ‘New Members’, ‘buddypress’ ); ?></option>

<?php do_action( ‘bp_activity_filter_options’ ); ?>

</select>
[/sourcecode]

As you can see, this part is checking if the component is active before displaying its actions which are then hardcoded in the template. If you are using, at least, BuddyPress 2.1 you can edit the templates to use the template tag bp_activity_show_filters() instead of the hardcoded options (except for the first one). Once done, the activity/index.php and members/single/activity.php will have a select tag looking like this:

[sourcecode language=”php”]
<select id="activity-filter-by">
<option value="-1"><?php _e( ‘&mdash; Everything &mdash;’, ‘buddypress’ ); ?></option>

<?php bp_activity_show_filters(); ?>

<?php do_action( ‘bp_activity_filter_options’ ); ?>

</select>
[/sourcecode]

For the groups/single/activity.php template, the only difference is that the bp_activity_show_filters() template tag is including a context parameter set to ‘group’ like this:

[sourcecode language=”php”]
<select id="activity-filter-by">
<option value="-1"><?php _e( ‘&mdash; Everything &mdash;’, ‘buddypress’ ); ?></option>

<?php bp_activity_show_filters( ‘group’ ); ?>

<?php do_action( ‘bp_group_activity_filter_options’ ); ?>
</select>
[/sourcecode]

Top ↑

Why using bp_activity_show_filters() within your theme’s templates Why using bp_activity_show_filters() within your theme’s templates

As soon as you are using at least version 2.1 of BuddyPress, you should consider using this new template tag as it will be used by BuddyPress to create new activity actions for its current (and future) components. BuddyPress plugins might also progressively rely on this template tag as setting activity actions have been greatly improved thanks to the function bp_activity_set_action().

Top ↑