Using WordPress ‘comment_flood_filter’ PHP filter

The comment_flood_filter WordPress PHP filter checks if a comment flood is occurring, with a default value of false.

Usage

add_filter('comment_flood_filter', 'your_custom_function', 10, 3);
function your_custom_function($bool, $time_lastcomment, $time_newcomment) {
    // your custom code here
    return $bool;
}

Parameters

  • $bool (bool): Whether a comment flood is occurring. Default false.
  • $time_lastcomment (int): Timestamp of when the last comment was posted.
  • $time_newcomment (int): Timestamp of when the new comment was posted.

More information

See WordPress Developer Resources: comment_flood_filter

Examples

Limit comment frequency to 1 comment per minute

This example prevents users from posting comments more frequently than once per minute.

add_filter('comment_flood_filter', 'limit_comment_frequency', 10, 3);
function limit_comment_frequency($bool, $time_lastcomment, $time_newcomment) {
    if (($time_newcomment - $time_lastcomment) < 60) {
        return true;
    }
    return false;
}

Allow admins to bypass comment flood restriction

This example allows administrators to bypass the comment flood restriction.

add_filter('comment_flood_filter', 'admin_bypass_comment_flood', 10, 3);
function admin_bypass_comment_flood($bool, $time_lastcomment, $time_newcomment) {
    if (current_user_can('manage_options')) {
        return false;
    }
    return $bool;
}

Disable comment flood check for specific post categories

This example disables the comment flood check for posts within specific categories.

add_filter('comment_flood_filter', 'disable_flood_check_for_categories', 10, 3);
function disable_flood_check_for_categories($bool, $time_lastcomment, $time_newcomment) {
    $post_id = get_the_ID();
    $categories = get_the_category($post_id);
    $allowed_categories = array('discussion', 'community');

    foreach ($categories as $category) {
        if (in_array($category->slug, $allowed_categories)) {
            return false;
        }
    }
    return $bool;
}

Log comment flood attempts

This example logs any comment flood attempts to a file.

add_filter('comment_flood_filter', 'log_comment_flood_attempts', 10, 3);
function log_comment_flood_attempts($bool, $time_lastcomment, $time_newcomment) {
    if ($bool) {
        $log_entry = sprintf(
            "Comment flood attempt at %s: Last: %d, New: %d\n",
            date('Y-m-d H:i:s', $time_newcomment),
            $time_lastcomment,
            $time_newcomment
        );
        error_log($log_entry, 3, 'comment_flood_log.txt');
    }
    return $bool;
}