Overview
This code snippet enables BuddyPress notification functionality to alert post authors when a new comment is posted on their blog post. BuddyPress provides a notification API that we can leverage to notify post authors about comments made on their posts.
Why Use BuddyPress Notification Functionality?
BuddyPress is a popular social networking plugin for WordPress that provides extended features like notifications. By using the BuddyPress notification system, you ensure that users are notified within their BuddyPress profile, keeping them engaged on the platform without needing external emails or systems. This approach keeps everything centralized, making it easier for the post author to manage interactions.
Code Snippet
The following code hooks into WordPress’s comment system and BuddyPress’s notification system to notify post authors when a new comment is added to their post.
// Function to notify post author of a new comment using BuddyPress notifications
function wbcom_notify_author_on_new_comment($comment_id, $comment_approved, $commentdata) {
// Check if the comment is approved
if (1 !== $comment_approved) {
return;
}
// Get the post ID and post author ID
$post_id = $commentdata['comment_post_ID'];
$author_id = get_post_field('post_author', $post_id);
$comment_link = get_comment_link($comment_id);
// Avoid notifying the author if they are the one who commented
if ($author_id == $commentdata['user_id']) {
return;
}
// Prepare the notification content
$comment_author = $commentdata['comment_author'];
$post_title = get_the_title($post_id);
$notification_message = sprintf(
'%s commented on your post "%s".',
$comment_author,
$post_title
);
// Add BuddyPress notification for the post author
bp_notifications_add_notification([
'user_id' => $author_id,
'item_id' => $post_id,
'secondary_item_id' => $comment_id,
'component_name' => 'comments',
'component_action' => 'new_comment',
'date_notified' => bp_core_current_time(),
'is_new' => 1,
]);
// Optional: Notify via email
wp_mail(
get_the_author_meta('user_email', $author_id),
'New Comment on Your Post',
$notification_message . ' View the comment: ' . $comment_link
);
}
// Hook into comment posting action
add_action('comment_post', 'wbcom_notify_author_on_new_comment', 10, 3);
Explanation
wbcom_notify_author_on_new_comment: This function triggers whenever a new comment is posted. It checks if the comment is approved and then proceeds to notify the post author.get_post_field('post_author', $post_id): Retrieves the post author’s ID.bp_notifications_add_notification: This BuddyPress function is used to add a notification for the post author.add_action('comment_post', 'wbcom_notify_author_on_new_comment', 10, 3): Hooks the function into the WordPress comment system, ensuring it runs when a comment is posted.
Benefits
- Centralized Notifications: Keeps all notifications inside the BuddyPress profile.
- Post Author Awareness: Ensures that the post author is immediately aware of new comments on their posts.
- Engagement: Encourages post authors to interact with their readers by notifying them of comments in real time.
Notes:
- This snippet can be added to your theme’s
functions.phpfile or as a custom plugin. - This code also includes optional email notifications.
