BuddyPress: Custom activity stream string for comments on custom post type (Quick Tip)

You have to add a filter to enable comments on the custom post type to be recorded by activity stream.

function bbg_record_my_custom_post_type_comments( $post_types ) {
	$post_types[] = 'booz'; // custom post type
	return $post_types;
}
add_filter( 'bp_blogs_record_comment_post_types', 'bbg_record_my_custom_post_type_comments' );

Now if someone comments on the post of post type booz, the activity stream will record it and it will record some thing like this “sunny commented on the post, post title” but it should be some thing like this “sunny commented on the booz, post title“. To make activity stream record a custom activity use the following code..

function bz_custom_activity_string($activity_action,  &$recorded_comment, $comment_link){
	// Get the user by the comment author email.
	$user = get_user_by( 'email', $recorded_comment->comment_author_email );
	// If user isn't registered, don't record activity
	if ( empty( $user ) )
		return $activity_action;
	// Get the user_id
	$user_id = (int) $user->ID;
	$post_permalink = get_permalink( $recorded_comment->comment_post_ID );
	$activity_action = sprintf( __( '%1$s commented on the booze, %2$s', 'buddypress' ), bp_core_get_userlink( $user_id ), '<a href="' . $post_permalink . '">' . apply_filters( 'the_title', $recorded_comment->post->post_title ) . '</a>' );
	return $activity_action;
}
add_filter( 'bp_blogs_activity_new_comment_action', 'bz_custom_activity_string', 10, 3 );

Tags: , ,