# 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.

- Type: PHP
- Runs: Run everywhere
- Category: [Security](https://wpide.com/library/category/security/)
- Tags: comments, spam, rate-limit
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/comment-daily-limit-per-ip/

## Code

```php
$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 use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Daily Comment Limit per Visitor" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
