Using Gravity Forms ‘gform_register_init_scripts’ PHP filter

The gform_register_init_scripts action allows you to tap into Gravity Forms “initialization scripts” functionality and register your own scripts to run inline with Gravity Forms. Scripts added via the GFFormDisplay::add_init_script() function will be output with the form(s) they are registered for.

Usage

add_action('gform_register_init_scripts', 'my_custom_function', 10, 2);

Parameters

  • $form (Form Object): The current form.
  • $field_values (array): If any field values were provided in the shortcode or functional call responsible for displaying this form, they will be available here.

More information

See Gravity Forms Docs: gform_register_init_scripts

Examples

Formatting Money Inputs

This example demonstrates how to use the gform_register_init_scripts action to register a script that formats money inputs using the GFFormDisplay::add_init_script() function.

add_action('gform_register_init_scripts', 'gform_format_money');

function gform_format_money($form) {
    $script = '(function($){' .
        '$(".gf_money input").each(function(){' .
        '$(this).val(gformFormatMoney($(this).val()));' .
        '}).change(function(){' .
        '$(this).val(gformFormatMoney($(this).val()));' .
        '});' .
        '})(jQuery);';

    GFFormDisplay::add_init_script($form['id'], 'format_money', GFFormDisplay::ON_PAGE_RENDER, $script);
}

What it does: The script will format all inputs with the class gf_money to display as money whenever their value changes.