The gform_footer_init_scripts_filter Gravity Forms PHP filter allows the scripts that fire in the footer to be modified.
Usage
A generic example of how to use the filter:
add_filter('gform_footer_init_scripts_filter', 'your_function_name', 10, 3);
Parameters
- $form_string (string): A string of the scripts used in the footer.
- $form (Form Object): The current form.
- $current_page (int): The ID of the current page.
More information
See Gravity Forms Docs: gform_footer_init_scripts_filter
Note: The gform_init_scripts_footer must be set to true so scripts are initialized in the footer of the site instead of the page body.
Examples
Add alert to footer
This example adds an alert to the footer when the form is loaded.
add_filter('gform_footer_init_scripts_filter', 'filter_footer', 10, 3); function filter_footer($form_string, $form, $current_page){ $form_string .= "<script type='text/javascript'>alert('Hello');</script>"; return $form_string; }
Target a specific form
This example targets a specific form with the form ID 1.
add_filter('gform_footer_init_scripts_filter_1', 'your_function_name', 10, 3);
Add Google Analytics event tracking
This example adds Google Analytics event tracking to a specific form with the form ID 2.
add_filter('gform_footer_init_scripts_filter_2', 'add_ga_event_tracking', 10, 3); function add_ga_event_tracking($form_string, $form, $current_page){ $form_string .= "<script type='text/javascript'>gtag('event', 'form_loaded', {'event_category': 'Forms', 'event_label': 'Form 2'});</script>"; return $form_string; }
Add custom JavaScript function
This example adds a custom JavaScript function that is executed when the form is submitted.
add_filter('gform_footer_init_scripts_filter', 'add_custom_js_function', 10, 3); function add_custom_js_function($form_string, $form, $current_page){ $form_string .= "<script type='text/javascript'>function customFunction() { console.log('Form submitted'); }</script>"; return $form_string; }
Add a custom jQuery action
This example adds a custom jQuery action to fade out the form after submission.
add_filter('gform_footer_init_scripts_filter', 'add_custom_jquery_action', 10, 3); function add_custom_jquery_action($form_string, $form, $current_page){ $form_string .= "<script type='text/javascript'>jQuery(document).on('submit', 'form', function() { jQuery(this).fadeOut(); });</script>"; return $form_string; }