Disallowing Activity recording for components or some activity types in BuddyPress
In BuddyPress, we have an action hook "bp_activity_before_save" that allows us to work with the activity object before it is saved in the database. The good thing is, After this hook is fired, BuddyPress checks for the component and type, and if any of these two is not set, BuddyPress will not record activity.
How do you stop recording activity:-
You hook to "bp_activity_before_save" and reset either component or type on the passed activity object.
Example 1:- Disallowing activity for certain component(s)
In this example, we will disallow recording of any activity related to groups component.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | function buddydev_exclude_component_activity_from_recording( &$activity ) { $component = 'groups'; //possible values 'members', 'groups', 'blogs', 'profile', 'friends' and so on... //we check for $activity->id to make sure we are working with new activity and not an existing one if ( empty( $activity->id ) && $component === $activity->component ) { //reset either component or type, both will cause a failure in saving activity $activity->component = ''; } } add_action( 'bp_activity_before_save', 'buddydev_exclude_component_activity_from_recording' ); |
That will stop BuddyPress from recording any group activity. You can modify the code to disallow multiple components to change the component.
Example 2:- Stop recording activity for certain activity types
In BuddyPress, each activity has certain type associated with it. A few example of types will be updated_profile, new_avatar, created_group, joined_group, group_details_updated, friendship_created.
In this example, we will be asking BuddyPress to not record activity when a profile is updated or user joins a group. Our types are "joined_group" and "updated_profile"
1 2 3 4 5 6 7 8 9 10 11 12 13 | function buddydev_exclude_activity_types_from_recording( &$activity ) { $excluded_types = array( 'joined_group', 'updated_profile' ); if ( empty( $activity->id ) && in_array( $activity->type , $excluded_types ) ) { //reset either component or type, both will cause a failure in saving activity $activity->type = ''; } } add_action( 'bp_activity_before_save', 'buddydev_exclude_activity_types_from_recording' ); |
You should put the code in your bp-custom.php and you will be good to go.
That's all for today folks. Good-bye until next time.
Thanks for this Brajesh,
This has proved to be very useful, Helps to keep the clutter from the activity page.
Regards
Ben
Thank you Ben. Glad it helped 🙂
Where can I get all the available activity types?
Please try running this against your BuddyPress database
SELECT DISTINCT(type) FROM wp_bp_activity
You may need to change the table prefix though bade on your installation.