The gform_localized_script_data_$name filter lets you modify the data being localized for a specific localized object $name in Gravity Forms.
Usage
Here’s an example of how to use the gform_localized_script_data_$name filter. We’re assuming ‘foobar’ as the $name in this instance:
add_filter( 'gform_localized_script_data_foobar', 'my_custom_function', 10, 3 );function my_custom_function( $localized_data, $script, $configs ) { // Your custom code here return $localized_data; }
Parameters
- $localized_data (array) – The current localized data.
- $script (string) – The script being localized.
- $configs (array) – An array of configurations applied to this script.
More information
See Gravity Forms Docs: gform_localized_script_data_$name
Examples
Modify localized data
If you want to modify a specific value within the localized data:
add_filter( 'gform_localized_script_data_foobar', 'modify_localized_data', 10, 3 );function modify_localized_data( $localized_data, $script, $configs ) { $localized_data['some_key'] = 'New Value'; // Changing value of some_key return $localized_data; }
Add new data
If you want to add a new piece of data to the localized data:
add_filter( 'gform_localized_script_data_foobar', 'add_new_data', 10, 3 );function add_new_data( $localized_data, $script, $configs ) { $localized_data['new_key'] = 'New Value'; // Adding new_key return $localized_data; }
Remove data
To remove a specific piece of data from the localized data:
add_filter( 'gform_localized_script_data_foobar', 'remove_data', 10, 3 );function remove_data( $localized_data, $script, $configs ) { unset($localized_data['some_key']); // Removing some_key return $localized_data; }
Check script name
To apply changes only for a specific script:
add_filter( 'gform_localized_script_data_foobar', 'specific_script', 10, 3 );function specific_script( $localized_data, $script, $configs ) { if ($script == 'my_script') { $localized_data['some_key'] = 'New Value'; // Changing value of some_key for 'my_script' } return $localized_data; }
Conditional change based on configs
To make changes based on the configurations:
add_filter( 'gform_localized_script_data_foobar', 'conditional_on_config', 10, 3 );function conditional_on_config( $localized_data, $script, $configs ) { if ($configs['some_config'] == 'expected_value') { $localized_data['some_key'] = 'New Value'; // Changing value of some_key if some_config is 'expected_value' } return $localized_data; }