Using WordPress ‘notify_moderator’ PHP filter

The notify_moderator WordPress PHP filter allows you to control whether the site moderator should receive email notifications for new comments, overriding the site settings.

Usage

add_filter('notify_moderator', 'my_custom_notify_moderator', 10, 2);

function my_custom_notify_moderator($maybe_notify, $comment_id) {
    // Your custom code here

    return $maybe_notify;
}

Parameters

  • $maybe_notify (bool): Whether to notify the blog moderator.
  • $comment_id (int): The ID of the comment for the notification.

More information

See WordPress Developer Resources: notify_moderator

Examples

Disable moderator notifications for all comments

Disable email notifications to the site moderator for all comments.

add_filter('notify_moderator', '__return_false');

Disable moderator notifications for comments from specific users

Disable email notifications to the site moderator for comments made by specific user IDs.

add_filter('notify_moderator', 'disable_notify_for_users', 10, 2);

function disable_notify_for_users($maybe_notify, $comment_id) {
    $comment = get_comment($comment_id);
    $user_ids_to_exclude = array(1, 2, 3);

    if (in_array($comment->user_id, $user_ids_to_exclude)) {
        return false;
    }
    return $maybe_notify;
}

Disable moderator notifications for comments on specific posts

Disable email notifications to the site moderator for comments made on specific post IDs.

add_filter('notify_moderator', 'disable_notify_for_posts', 10, 2);

function disable_notify_for_posts($maybe_notify, $comment_id) {
    $comment = get_comment($comment_id);
    $post_ids_to_exclude = array(100, 101, 102);

    if (in_array($comment->comment_post_ID, $post_ids_to_exclude)) {
        return false;
    }
    return $maybe_notify;
}

Enable moderator notifications only for pending comments

Send email notifications to the site moderator only for pending comments.

add_filter('notify_moderator', 'notify_only_pending_comments', 10, 2);

function notify_only_pending_comments($maybe_notify, $comment_id) {
    $comment = get_comment($comment_id);

    if ('0' === $comment->comment_approved) {
        return true;
    }
    return false;
}

Disable moderator notifications for comments containing specific words

Disable email notifications to the site moderator for comments containing specific words.

add_filter('notify_moderator', 'disable_notify_for_specific_words', 10, 2);

function disable_notify_for_specific_words($maybe_notify, $comment_id) {
    $comment = get_comment($comment_id);
    $words_to_exclude = array('spam', 'viagra', 'fake');

    foreach ($words_to_exclude as $word) {
        if (stripos($comment->comment_content, $word) !== false) {
            return false;
        }
    }
    return $maybe_notify;
}