I am working on a customer’s website that has Gravity Forms installed.
I came here to report that on his website, we are still getting the inline script for the “gform_main_scripts_loaded” loaded on all pages.
SImilar case: Remove unused GF script from header [RESOLVED]
Following the Gravity Code execution flow, I end up here: /gravityforms.php
/**
* Use wp_add_inline_script to output the hooks JS programmatically.
*
* @since 2.5.2
* @access public
*/
public static function load_hooks_with_inline_script() {
$needed = GFCommon::requires_gf_hooks_javascript();
if ( ! $needed ) {
return;
}
$hooks_code = GFCommon::get_hooks_javascript_code();
wp_add_inline_script( 'gform_gravityforms', $hooks_code, 'before' );
}
It has passed through all the previous function calls and the various conditional checks and results in calling the load_hooks_with_inline_script.
For now, I will handle the situation by adding a custom filter on this function, to handle the loading of the script conditionally outside of the gravity forms code and will inform the customer that this change needs to be preserved in future updates of GF, if there will be no other solution real soon.
/**
* Use wp_add_inline_script to output the hooks JS programmatically.
*
* @since 2.5.2
* @access public
*/
public static function load_hooks_with_inline_script() {
$needed = GFCommon::requires_gf_hooks_javascript();
if ( ! $needed ) {
return;
}
$shouldInjectTheScript = apply_filters( 'gform_should_inject_inline_script', true);
if ($shouldInjectTheScript) {
$hooks_code = GFCommon::get_hooks_javascript_code();
wp_add_inline_script( 'gform_gravityforms', $hooks_code, 'before' );
}
}
If anyone else arrives here:
You can filter the value in your functions.php like so:
add_filter('gform_should_inject_inline_script', function ($should_inject) {
// Your custom logic to determine if the script should be injected
// Example: disable it in single post pages.
if (is_single()) {
$should_inject = false;
}
return $should_inject;
}, 10);