Daily Comment Limit per Visitor
Holds comments for moderation once a single IP address has posted more than a set number of comments in 24 hours, which stops comment floods and spam runs.
Opens WPIDE on
Code
$wpide_comment_daily_max = 10;
add_filter( 'pre_comment_approved', function ( $approved, $commentdata ) use ( $wpide_comment_daily_max ) {
global $wpdb;
if ( 'spam' === $approved || is_user_logged_in() || empty( $commentdata['comment_author_IP'] ) ) {
return $approved;
}
$count = (int) $wpdb->get_var( $wpdb->prepare(
"SELECT COUNT(*) FROM {$wpdb->comments} WHERE comment_author_IP = %s AND comment_date_gmt > %s",
$commentdata['comment_author_IP'],
gmdate( 'Y-m-d H:i:s', time() - DAY_IN_SECONDS )
) );
return $count >= $wpide_comment_daily_max ? 0 : $approved;
}, 20, 2 );
Notes
Logged-in users are not limited; extra comments are held, not rejected, so you can still approve genuine ones.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.