Advertisement
vapvarun

Record a new blog comment as an activity in BuddyPress

Dec 14th, 2023
1,089
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.17 KB | Software | 0 0
  1. /**
  2.  * Record a new blog comment as an activity in BuddyPress.
  3.  *
  4.  * @param int $comment_ID The comment ID.
  5.  * @param int $comment_approved 1 if the comment is approved, 0 if not.
  6.  */
  7. function wbcom_record_blog_comment_as_activity( $comment_ID, $comment_approved ) {
  8.     if ( 1 !== $comment_approved ) {
  9.         return;
  10.     }
  11.  
  12.     $comment = get_comment( $comment_ID );
  13.     $user_id = $comment->user_id;
  14.     $user_info = get_userdata($user_id);
  15.     $username = $user_info->user_nicename;
  16.     $post_id = $comment->comment_post_ID;
  17.     $post_title = get_the_title( $post_id );
  18.     $comment_link = get_comment_link( $comment_ID );
  19.     $content = sprintf( 'Commented on the post, <a href="%s">%s</a>: "%s"', $comment_link, $post_title, $comment->comment_content );
  20.  
  21.     bp_activity_add( array(
  22.         'user_id'   => $user_id,
  23.         'action'    => sprintf( '%s posted a new comment on the post, <a href="%s">%s</a>', $username, get_permalink($post_id), $post_title ),
  24.         'content'   => $content,
  25.         'component' => 'activity',
  26.         'type'      => 'new_blog_comment',
  27.     ) );
  28. }
  29. add_action( 'comment_post', 'wbcom_record_blog_comment_as_activity', 10, 2 );
  30.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement