Blog / WordPress/ WordPress Tutorial: Limit Comment Flooding with Pure Code

WordPress Tutorial: Limit Comment Flooding with Pure Code

WordPress 教程:使用纯代码限制重复评论的间隔时间

Limit Comment Flooding with Pure Code in WordPress

WordPress comments are a vital way to interact with visitors, but they can be abused by spam or flooding. To maintain order and quality, limiting the time between consecutive comments is an effective solution. This tutorial shows how to add a simple code snippet to enforce a minimum comment interval.

Code Implementation

Add the following code to your active theme's functions.php file. It checks the time between a user's comments and blocks submissions if the interval is too short (e.g., less than 90 seconds).

add_filter('comment_flood_filter', 'limit_comment_flood_interval', 10, 3);
function limit_comment_flood_interval($flood_control, $time_last, $time_new) {
    // Minimum interval in seconds
    $interval_seconds = 90;
    $time_diff = $time_new - $time_last;
    if ( $time_diff < $interval_seconds ) {
        $remaining_time = $interval_seconds - $time_diff;
        return new WP_Error( 'comment_flood', sprintf( 'Please wait %d seconds before posting another comment.', $remaining_time ) );
    }
    return false;
}

How to Use

Steps:

  1. Go to Appearance → Theme File Editor in your WordPress admin.
  2. Select functions.php from the file list on the right.
  3. Paste the code at the end of the file.
  4. Click Update File to save.

Customization:

  • Adjust the $interval_seconds value (in seconds) to change the minimum wait time.
  • Modify the error message inside the sprintf() function as needed.

Notes:

  • This works with the default WordPress comment system.
  • Always back up your functions.php file before editing.
  • The limit applies to both logged‑in users and guests.
  • If you use a caching plugin, clear the cache after adding the code.

This simple addition helps prevent comment flooding and improves moderation efficiency.

Post a Comment

Your email will not be published. Required fields are marked with *.