Halloween Sale 15% off Ends in See the plans
PHP

Minimum Message Length in WPForms

Requires WPForms paragraph fields to contain at least a few words, so you get enough detail to reply and fewer one-word junk messages.

Don’t have WPIDE yet? Install it on your site

Code

$wpide_wpf_min_words = 5;

add_action( 'wpforms_process_validate_textarea', function ( $field_id, $field_submit, $form_data ) use ( $wpide_wpf_min_words ) {
    $text     = trim( wp_strip_all_tags( (string) $field_submit ) );
    $required = ! empty( $form_data['fields'][ $field_id ]['required'] );

    if ( '' === $text && ! $required ) {
        return;
    }

    if ( preg_match_all( '/[\p{L}\p{N}\']+/u', $text ) < $wpide_wpf_min_words ) {
        $process = method_exists( wpforms(), 'obj' ) ? wpforms()->obj( 'process' ) : wpforms()->process;
        $process->errors[ $form_data['id'] ][ $field_id ] = sprintf( 'Please write at least %d words so we can help.', $wpide_wpf_min_words );
    }
}, 10, 3 );

Notes

Empty optional fields are skipped; required fields must reach the minimum.

How to add this snippet

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.