Using WordPress ‘check_comment_flood’ PHP action

The check_comment_flood WordPress PHP action allows you to check for comment flooding before a comment is marked approved.

Usage

add_action('check_comment_flood', 'your_custom_function', 10, 4);

function your_custom_function($comment_author_ip, $comment_author_email, $comment_date_gmt, $wp_error) {
    // Your custom code here

    return $comment_author_ip;
}

Parameters

  • $comment_author_ip (string): Comment author’s IP address.
  • $comment_author_email (string): Comment author’s email.
  • $comment_date_gmt (string): GMT date the comment was posted.
  • $wp_error (bool): Whether to return a WP_Error object instead of executing wp_die() or die() if a comment flood is occurring.

More information

See WordPress Developer Resources: check_comment_flood

Examples

Limit comments per minute from the same IP

This example limits the number of comments per minute from the same IP address to prevent flooding.

add_action('check_comment_flood', 'limit_comments_per_minute', 10, 4);

function limit_comments_per_minute($comment_author_ip, $comment_author_email, $comment_date_gmt, $wp_error) {
    // Set the time limit and maximum comments allowed
    $time_limit = 60; // in seconds
    $max_comments = 5;

    // Get recent comments from the same IP
    $comments = get_comments(array(
        'author_ip' => $comment_author_ip,
        'date_query' => array(
            'after' => "{$time_limit} seconds ago",
        ),
    ));

    // Check if the number of comments exceeds the limit
    if (count($comments) >= $max_comments) {
        if ($wp_error) {
            return new WP_Error('comment_flood', 'Too many comments in a short period. Please try again later.');
        } else {
            wp_die('Too many comments in a short period. Please try again later.');
        }
    }

    return $comment_author_ip;
}

Block comments from specific IP addresses

This example blocks comments from specific IP addresses.

add_action('check_comment_flood', 'block_specific_ips', 10, 4);

function block_specific_ips($comment_author_ip, $comment_author_email, $comment_date_gmt, $wp_error) {
    // List of blocked IP addresses
    $blocked_ips = array('123.45.67.89', '987.65.43.21');

    // Check if the author IP is in the blocked list
    if (in_array($comment_author_ip, $blocked_ips)) {
        if ($wp_error) {
            return new WP_Error('blocked_ip', 'Your IP address is not allowed to post comments.');
        } else {
            wp_die('Your IP address is not allowed to post comments.');
        }
    }

    return $comment_author_ip;
}

Block comments with specific email domains

This example blocks comments from specific email domains.

add_action('check_comment_flood', 'block_specific_email_domains', 10, 4);

function block_specific_email_domains($comment_author_ip, $comment_author_email, $comment_date_gmt, $wp_error) {
    // List of blocked email domains
    $blocked_domains = array('example.com', 'baddomain.com');

    // Check if the author email domain is in the blocked list
    $email_domain = substr(strrchr($comment_author_email, "@"), 1);
    if (in_array($_email_domain, $blocked_domains)) {
if ($wp_error) {
return new WP_Error('blocked_email_domain', 'Your email domain is not allowed to post comments.');
} else {
wp_die('Your email domain is not allowed to post comments.');
}
}
return $comment_author_ip;
}

Limit comments per day from the same email address

This example limits the number of comments per day from the same email address to prevent flooding.

add_action('check_comment_flood', 'limit_comments_per_day_from_email', 10, 4);

function limit_comments_per_day_from_email($comment_author_ip, $comment_author_email, $comment_date_gmt, $wp_error) {
// Set the time limit and maximum comments allowed
$max_comments = 10;
// Get recent comments from the same email address
$comments = get_comments(array(
    'author_email' => $comment_author_email,
    'date_query' => array(
        'after' => '1 day ago',
    ),
));

// Check if the number of comments exceeds the limit
if (count($comments) >= $max_comments) {
    if ($wp_error) {
        return new WP_Error('comment_flood', 'Too many comments in a short period. Please try again later.');
    } else {
        wp_die('Too many comments in a short period. Please try again later.');
    }
}

return $comment_author_ip;
}

Require a minimum time between comments from the same user

This example requires a minimum time between comments from the same user.

add_action('check_comment_flood', 'minimum_time_between_comments', 10, 4);

function minimum_time_between_comments($comment_author_ip, $comment_author_email, $comment_date_gmt, $wp_error) {
// Set the minimum time between comments (in seconds)
$min_time = 60;

// Get the latest comment from the same user
$latest_comment = get_comments(array(
    'author_email' => $comment_author_email,
    'number' => 1,
    'orderby' => 'comment_date_gmt',
    'order' => 'DESC',
));

if ($latest_comment) {
    $time_since_last_comment = strtotime($comment_date_gmt) - strtotime($latest_comment[0]->comment_date_gmt);

    // Check if the time since the last comment is less than the minimum time
    if ($time_since_last_comment < $min_time) {
        if ($wp_error) {
            return new WP_Error('comment_flood', 'Please wait before posting another comment.');
        } else {
            wp_die('Please wait before posting another comment.');
        }
    }
}

return $comment_author_ip;
}