Add custom recipient to WordPress comment notification

WordPress sends email notification to Comment Post Author by default. And if it is a reply, it also send a notification to the comment author.

So, what if we need to send email to a different person, using some different logic?

In that case we can use WP’s corresponding filter, called comment_notification_recipients.

This filter sits in wp-includes/pluggable.php.

So, we just add this small snippet to functions.php file of our active theme:

add_filter( 'comment_notification_recipients',function( $emails, $comment_ID){
    $emails[]='[email protected]';
    return $emails;
},10,2);

What if this email should be fetched by using some logic, f.e. there is special meta field in each post, where we need to fetch it from. Here is how it should look like:

add_filter( 'comment_notification_recipients',function( $emails, $comment_ID){
    $comment_obj=get_comment($comment_ID);
    if(!empty($comment_obj->comment_post_ID)){
        $email_owner=get_post_meta($comment_obj->comment_post_ID,
                      'email_meta_field',true); 
        if(is_email($email_owner))$emails[]=$email_owner;
    }
    return $emails;
},10,2);

That’s all. If any question on this subject, you can leave a reply. Don’t forget to subscribe my newsletter feed club to get more and more useful small snippets for WP websites.