gform_counter_script

Description

Use this filter to change the initialization script for the textarea counter script. Can be used to specify different initialization parameters.

Usage

add_filter( 'gform_counter_script', 'set_counter_script', 10, 5 );

You can also target a specific form by adding 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 current form.

  • $input_id string

    ID of HTML input.

  • $maxLength integer

    Configured max character length.

  • $field GF_Field

    The Field object.

Examples

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:

https://github.com/LeadSift/jquery-textareaCounter

Placement

This code should be placed in the functions.php file of your active theme.

Source Code

This filter is located in GFFormDisplay::get_counter_init_script() in form_display.php