# Minimum and Maximum Comment Length

> Rejects comments shorter than 15 characters or longer than 5,000 characters. Moderators are not limited.

- Type: PHP
- Runs: Run everywhere
- Category: [Comments](https://wpide.com/library/category/comments/)
- Tags: comments, spam, validation
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/comment-length-limits/

## Code

```php
add_filter( 'preprocess_comment', function ( $commentdata ) {
    $min = 15;
    $max = 5000;

    $type = isset( $commentdata['comment_type'] ) ? $commentdata['comment_type'] : '';

    if ( ( '' !== $type && 'comment' !== $type ) || current_user_can( 'moderate_comments' ) ) {
        return $commentdata;
    }

    $text   = trim( wp_strip_all_tags( (string) $commentdata['comment_content'] ) );
    $length = function_exists( 'mb_strlen' ) ? mb_strlen( $text ) : strlen( $text );

    if ( $length < $min ) {
        wp_die( esc_html( sprintf( 'Your comment is too short. Please write at least %d characters.', $min ) ), 'Comment too short', array( 'response' => 400, 'back_link' => true ) );
    }

    if ( $length > $max ) {
        wp_die( esc_html( sprintf( 'Your comment is too long. Please keep it under %d characters.', $max ) ), 'Comment too long', array( 'response' => 400, 'back_link' => true ) );
    }

    return $commentdata;
} );
```

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Minimum and Maximum Comment Length" 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/
