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:
- Go to Appearance → Theme File Editor in your WordPress admin.
- Select
functions.phpfrom the file list on the right. - Paste the code at the end of the file.
- Click Update File to save.
Customization:
- Adjust the
$interval_secondsvalue (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.phpfile 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.