gform_register_init_scripts

Description

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

Applies to all forms.

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

You can also specify this per form by adding the form id (123 in this example) after the hook name.

add_action( 'gform_register_init_scripts_123', '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.

Examples

This example (via Gravity Wiz), demonstrates how the gform_register_init_scripts hook can be used to register your GF-related initialization scripts 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 );
}

Placement

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

Source Code

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