The comment_notification_recipients WordPress PHP filter allows you to modify the list of email addresses that receive a comment notification.
Usage
add_filter('comment_notification_recipients', 'your_custom_function', 10, 2);
function your_custom_function($emails, $comment_id) {
// your custom code here
return $emails;
}
Parameters
$emails(string[]): An array of email addresses to receive a comment notification.$comment_id(string): The comment ID as a numeric string.
More information
See WordPress Developer Resources: comment_notification_recipients
Examples
Add a site admin’s email to the list of recipients
Add the site admin’s email to the list of recipients for comment notifications.
add_filter('comment_notification_recipients', 'add_site_admin_email', 10, 2);
function add_site_admin_email($emails, $comment_id) {
$admin_email = get_option('admin_email');
$emails[] = $admin_email;
return $emails;
}
Notify all registered users of new comments
Send a comment notification to all registered users on the site.
add_filter('comment_notification_recipients', 'notify_all_registered_users', 10, 2);
function notify_all_registered_users($emails, $comment_id) {
$users = get_users(['fields' => ['user_email']]);
foreach ($users as $user) {
$emails[] = $user->user_email;
}
return $emails;
}
Notify only users with a specific role
Send a comment notification only to users with a specific role (e.g., ‘editor’).
add_filter('comment_notification_recipients', 'notify_specific_role_users', 10, 2);
function notify_specific_role_users($emails, $comment_id) {
$role = 'editor';
$users = get_users(['role' => $role, 'fields' => ['user_email']]);
foreach ($users as $user) {
$emails[] = $user->user_email;
}
return $emails;
}
Exclude a specific email address from notifications
Prevent a specific email address from receiving comment notifications.
add_filter('comment_notification_recipients', 'exclude_specific_email', 10, 2);
function exclude_specific_email($emails, $comment_id) {
$exclude_email = '[email protected]';
$emails = array_diff($emails, [$exclude_email]);
return $emails;
}
Add custom email addresses to the notification list
Add custom email addresses to the list of recipients for comment notifications.
add_filter('comment_notification_recipients', 'add_custom_emails', 10, 2);
function add_custom_emails($emails, $comment_id) {
$custom_emails = ['[email protected]', '[email protected]'];
$emails = array_merge($emails, $custom_emails);
return $emails;
}