vapvarun icon

Record a new blog comment as an activity in BuddyPress

vapvarun | PRO | 12/14/23 10:38:24 AM UTC | 0 ⭐ | 1605 👁️ | Never ⏰ | []
PHP |

1.17 KB

|

Software

|

0 👍

/

0 👎

/**
 * Record a new blog comment as an activity in BuddyPress.
 *
 * @param int $comment_ID The comment ID.
 * @param int $comment_approved 1 if the comment is approved, 0 if not.
 */
function wbcom_record_blog_comment_as_activity( $comment_ID, $comment_approved ) {
    if ( 1 !== $comment_approved ) {
        return;
    }
 
    $comment = get_comment( $comment_ID );
    $user_id = $comment->user_id;
    $user_info = get_userdata($user_id);
    $username = $user_info->user_nicename;
    $post_id = $comment->comment_post_ID;
    $post_title = get_the_title( $post_id );
    $comment_link = get_comment_link( $comment_ID );
    $content = sprintf( 'Commented on the post, <a href="%s">%s</a>: "%s"', $comment_link, $post_title, $comment->comment_content );
 
    bp_activity_add( array(
        'user_id'   => $user_id,
        'action'    => sprintf( '%s posted a new comment on the post, <a href="%s">%s</a>', $username, get_permalink($post_id), $post_title ),
        'content'   => $content,
        'component' => 'activity',
        'type'      => 'new_blog_comment',
    ) );
}
add_action( 'comment_post', 'wbcom_record_blog_comment_as_activity', 10, 2 );
 

Comments