# Minimum Message Length in Contact Form 7

> Requires message fields to contain a few real words, which stops one-word and empty-looking submissions and gives you enough detail to reply.

- Type: PHP
- Runs: Run everywhere
- Category: [Forms](https://wpide.com/library/category/forms/)
- Tags: contact-form-7, validation, textarea
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/cf7-message-min-length/

## Code

```php
$wpide_cf7_min_words = 5; // Minimum number of words in textarea fields.

$wpide_cf7_words_check = function ( $result, $tag ) use ( $wpide_cf7_min_words ) {
    $text = isset( $_POST[ $tag->name ] ) ? trim( wp_strip_all_tags( (string) wp_unslash( $_POST[ $tag->name ] ) ) ) : '';

    if ( '' === $text && ! $tag->is_required() ) {
        return $result;
    }

    $words = preg_match_all( '/[\p{L}\p{N}\']+/u', $text );

    if ( $words < $wpide_cf7_min_words ) {
        $result->invalidate( $tag, sprintf( 'Please write at least %d words so we can help you.', $wpide_cf7_min_words ) );
    }

    return $result;
};

add_filter( 'wpcf7_validate_textarea', $wpide_cf7_words_check, 20, 2 );
add_filter( 'wpcf7_validate_textarea*', $wpide_cf7_words_check, 20, 2 );
```

## Notes

CF7 also has a minlength: option per tag; this snippet sets a site-wide minimum in words instead of characters.

## How to use it

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