Using Gravity Forms ‘gforms_localized_script_data_remove’ PHP filter

The gform_localized_script_data_$name Gravity Forms PHP filter allows users to filter the data being localized for a given localized object $name.

Usage

add_filter('gform_localized_script_data_foobar', 'your_function_name', 10, 3);

Parameters

  • $localized_data (array) – The current localized data.
  • $script (string) – The script being localized.
  • $configs (array) – An array of $configs being applied to this script.

More information

See Gravity Forms Docs: gform_localized_script_data_$name

Examples

Include option value in the localized data

This example adds the value from get_option('foobar') to the localized data for the foobar object.

add_filter('gforms_localized_script_data_foobar', function($localized_data, $script, $configs) {
    $localized_data['foobar_option'] = get_option('foobar');
    return $localized_data;
}, 10, 3);

Modify localized data based on configuration

This example changes the localized data based on a specific configuration.

add_filter('gforms_localized_script_data_example', function($localized_data, $script, $configs) {
    if (isset($configs['example_config'])) {
        $localized_data['example_data'] = $configs['example_config'];
    }
    return $localized_data;
}, 10, 3);

Add custom values to localized data

This example adds custom values to the localized data.

add_filter('gforms_localized_script_data_custom', function($localized_data, $script, $configs) {
    $localized_data['custom_data_1'] = 'Custom Value 1';
    $localized_data['custom_data_2'] = 'Custom Value 2';
    return $localized_data;
}, 10, 3);

Remove values from localized data

This example removes specific values from the localized data.

add_filter('gforms_localized_script_data_remove', function($localized_data, $script, $configs) {
    unset($localized_data['value_to_remove']);
    return $localized_data;
}, 10, 3);

Conditionally modify localized data

This example modifies the localized data based on a condition.

add_filter('gforms_localized_script_data_conditional', function($localized_data, $script, $configs) {
    if (current_user_can('manage_options')) {
        $localized_data['admin_data'] = 'Data for Admins';
    }
    return $localized_data;
}, 10, 3);