Using WordPress ‘comment_moderation_recipients’ PHP filter

The comment_moderation_recipients WordPress PHP filter allows you to modify the list of recipients for comment moderation emails.

Usage

add_filter('comment_moderation_recipients', 'your_custom_function', 10, 2);

function your_custom_function($emails, $comment_id) {
    // your custom code here

    return $emails;
}

Parameters

  • $emails (string[]): List of email addresses to notify for comment moderation.
  • $comment_id (int): Comment ID.

More information

See WordPress Developer Resources: comment_moderation_recipients

Examples

Add a new recipient

Add a new email address to the list of recipients for comment moderation emails.

add_filter('comment_moderation_recipients', 'add_new_recipient', 10, 2);

function add_new_recipient($emails, $comment_id) {
    $new_email = '[email protected]';
    $emails[] = $new_email;

    return $emails;
}

Remove a specific recipient

Remove a specific email address from the list of recipients for comment moderation emails.

add_filter('comment_moderation_recipients', 'remove_specific_recipient', 10, 2);

function remove_specific_recipient($emails, $comment_id) {
    $email_to_remove = '[email protected]';
    $emails = array_diff($emails, array($email_to_remove));

    return $emails;
}

Replace all recipients

Replace all existing recipients with a new list of email addresses for comment moderation emails.

add_filter('comment_moderation_recipients', 'replace_all_recipients', 10, 2);

function replace_all_recipients($emails, $comment_id) {
    $new_emails = array('[email protected]', '[email protected]');
    $emails = $new_emails;

    return $emails;
}

Send to author only if the comment is on their post

Send comment moderation emails only to the post author if the comment is on their post.

add_filter('comment_moderation_recipients', 'send_to_author_only', 10, 2);

function send_to_author_only($emails, $comment_id) {
    $comment = get_comment($comment_id);
    $post = get_post($comment->comment_post_ID);
    $author_email = get_the_author_meta('email', $post->post_author);

    return array($author_email);
}

Exclude recipients based on comment content

Exclude specific recipients from receiving comment moderation emails if the comment contains certain words.

add_filter('comment_moderation_recipients', 'exclude_recipients_based_on_content', 10, 2);

function exclude_recipients_based_on_content($emails, $comment_id) {
    $comment = get_comment($comment_id);
    $comment_content = strtolower($comment->comment_content);

    $words_to_check = array('word1', 'word2');

    foreach ($words_to_check as $word) {
        if (strpos($comment_content, $word) !== false) {
            $email_to_remove = '[email protected]';
            $emails = array_diff($emails, array($email_to_remove));
            break;
        }
    }

    return $emails;
}