Change recipient of email notifications for new comments in WordPress using comment_notification_recipients filter

Client’s website I was recently working on required the email notifications for new comments to be sent to the admin only and not the post author.

If you turn on the email notifications for new comments in WordPress, the post author will receive an email every time someone posts a comment on his or her post (‘Email me whenever Anyone posts a comment’ option).

New comment notifications and comment moderation options can be found in Settings > Discussion.

WordPress comments options- Email me whenever anyone posts a comment

You can find more details about the comment options in the WordPress Codex.

By default, whenever anyone posts a new comment, WordPress will notify the post author only.

However, a client’s website I was recently working on required the email notifications for new comments to be sent to the admin only and not the post author.

This can be achieved using the comment_notification_recipients filter.

comment_notification_recipients

The comment_notification_recipients filter allows you to filter the list of email addresses that will receive an email notification from WordPress about new comments. This enables you to not only add new email addresses that will be notified but also to remove or change them, depending on your requirements.

Sending new comment notifications to admin only

Below is a function that will send all new comment notifications to admin email address only, using the comment_notification_recipients filter.

function if_change_recipient_comment_notification( $emails, $comment_id ){
  $admin_email = get_bloginfo('admin_email');

  return array( $admin_email );
}
add_filter( 'comment_notification_recipients', 'if_change_recipient_comment_notification', 10, 2 );

In the above code, we are replacing the default array of email addresses passed to the function ($emails) with our own array. As we only want to send the new comment notifications to the admin, we only return the site’s admin email address. You can get the admin’s email using the WordPress get_bloginfo() function.

The above code could easily be changed to send the new comment notifications to any email address, including one that is not associated with the website. Below is a function that allows you to send new comment notifications to any email address.

 function if_change_recipient_comment_notification( $emails, $comment_id ){
 $recipient_email = 'example@example.com';
 
 return array( $recipient_email );
}
add_filter( 'comment_notification_recipients', 'if_change_recipient_comment_notification', 10, 2 ); 

 

Further reading

WordPress Codex: Settings, Discussion Screen