To add BuddyPress page and post comments to the activity stream, you can use the bp_activity_add()
function. This function allows you to add an activity item to the activity stream with a specific action, content, and type.
Here’s an example of how you can use the bp_activity_add()
function to add page and post comments to the activity stream:
// Add BuddyPress page and post comments in activity stream
// More snippets at wpunplugged.com
/**
* @param int $comment_id The ID of the comment.
*/
function my_bp_add_comment_activity( $comment_id ) {
// Get the comment object
$comment = get_comment( $comment_id );
// Check if the comment is approved
if ( $comment->comment_approved ) {
// Get the post object
$post = get_post( $comment->comment_post_ID );
// Set the activity action
$action = sprintf( __( '%s commented on the %s %s', 'my-text-domain' ), bp_core_get_userlink( $comment->user_id ), $post->post_type, '<a href="' . get_permalink( $post ) . '">' . esc_html( $post->post_title ) . '</a>' );
// Set the activity content
$content = $comment->comment_content;
// Set the activity type
$type = 'new_comment';
// Add the activity item to the activity stream
bp_activity_add( array(
'user_id' => $comment->user_id,
'action' => $action,
'content' => $content,
'primary_link' => get_permalink( $post ),
'type' => $type,
) );
}
}
add_action( 'wp_insert_comment', 'my_bp_add_comment_activity' );
This code will add a new activity item to the activity stream for every approved page and post comment that is added to your site. The activity item will include the comment action, content, and type.
To add this code to your site, you can add it to your theme’s functions.php
file.
Note that this code assumes that you have the BuddyPress plugin installed and activated on your WordPress site. If you don’t have BuddyPress installed, you will need to install and activate it before you can use the bp_activity_add()
function.