Skip to content

Instantly share code, notes, and snippets.

@adczk
Created January 25, 2023 10:19
Show Gist options
  • Select an option

  • Save adczk/a00fb9581668110fdb82644c0176f180 to your computer and use it in GitHub Desktop.

Select an option

Save adczk/a00fb9581668110fdb82644c0176f180 to your computer and use it in GitHub Desktop.
Forminator - textarea minimum words limit
<?php
/**
* Plugin Name: [Forminator] Set minimum number of words limit for textarea field
* Plugin URI: https://premium.wpmudev.org/
* Description: Sets minium number of words for textarea field
* Author: adczk
* Author URI: https://premium.wpmudev.org/
* License: GPLv2 or later
*
* Tested with Forminator 1.22.1
*
* config explained in code comment
*/
add_filter( 'forminator_custom_form_submit_errors', 'check_textarea_minimum_words', 99, 3 );
function check_textarea_minimum_words( $submit_errors, $form_id, $field_data_array ) {
# CONFIG HERE
$form_ids = array( 2245 ); // IDs of forms to check; if more than one, separate by comma
$text_field = 'textarea-1'; // textarea field ID
$text_words = 10; // minimum number of words to allow
$error_msg = 'Text too short!'; // error message
# END OF CONFIG
if ( in_array( $form_id, $form_ids ) ) {
$words = 0;
foreach( $field_data_array as $arr ) {
if ( $arr['name'] == $text_field ) {
$text_val = $arr['value'];
$words = preg_split( '/[\s]+/', $arr['value'] );
if ( is_array( $words ) && count( $words ) < $text_words ) {
$submit_errors[][$text_field] = $error_msg;
}
}
}
}
return $submit_errors;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment