# Character Counter Under the Comment Box

> Shows how many characters are left while typing a comment and stops input at the limit you set.

- Type: PHP
- Runs: Frontend only
- Category: [Comments](https://wpide.com/library/category/comments/)
- Tags: comments, form, ux
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/comment-character-counter/

## Code

```php
// Maximum comment length in characters.
$wpide_comment_max_chars = 1500;

add_filter( 'comment_form_field_comment', function ( $field ) use ( $wpide_comment_max_chars ) {
    $field = preg_replace( '/<textarea\b/', '<textarea maxlength="' . (int) $wpide_comment_max_chars . '"', $field, 1 );

    return $field . '<p class="wpide-comment-counter" aria-live="polite" style="font-size:.85em;opacity:.75;margin-top:.25em"></p>';
} );

add_action( 'wp_footer', function () use ( $wpide_comment_max_chars ) {
    if ( ! is_singular() || ! comments_open() ) {
        return;
    }
    ?>
    <script>
    ( function () {
        var field = document.getElementById( 'comment' );
        var out = document.querySelector( '.wpide-comment-counter' );
        if ( ! field || ! out ) {
            return;
        }
        var max = <?php echo (int) $wpide_comment_max_chars; ?>;
        function update() {
            out.textContent = ( max - field.value.length ) + ' characters left';
        }
        field.addEventListener( 'input', update );
        update();
    } )();
    </script>
    <?php
} );
```

## Notes

Pair it with the minimum and maximum comment length snippet for a server-side check.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Character Counter Under the Comment Box" 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/
