The gform_counter_script filter allows you to modify the initialization script for the textarea counter script, enabling you to set different initialization parameters.
Usage
add_filter('gform_counter_script', 'set_counter_script', 10, 5);
To target a specific form, add the form ID after the hook name:
add_filter('gform_counter_script_6', 'set_counter_script', 10, 5);
Parameters
- $script (string): The script (including
<script>tag) to be filtered. - $form_id (integer): ID of the current form.
- $input_id (string): ID of the HTML input.
- $maxLength (integer): Configured max character length.
- $field (GF_Field): The Field object.
More information
See Gravity Forms Docs: gform_counter_script
Examples
Change the counter message format
This example changes the display format of the counter message to: “XXX characters remaining”
add_filter('gform_counter_script_187', 'set_counter_script', 10, 5);
function set_counter_script($script, $form_id, $input_id, $max_length, $field) {
$script = "jQuery('#{$input_id}').textareaCount(" .
" {" .
" 'maxCharacterSize': {$max_length}," .
" 'originalStyle': 'ginput_counter'," .
" 'displayFormat' : '#left characters remaining.'" .
" });";
return $script;
}
A full list of available initialization options can be found at: jQuery Textarea Counter
Place this code in the functions.php file of your active theme. The source code for this filter is located in GFFormDisplay::get_counter_init_script() in form_display.php.