I am including jQuery already on my website, but when using some conditional logic with Gravity Forms, it includes jQuery again, as well as its own scripts of course.
Output looks like:
<script type='text/javascript' src='//localhost:8080/wp-includes/js/jquery/jquery.js?ver=1.11.3'></script>
<script type='text/javascript' src='//localhost:8080/wp-includes/js/jquery/jquery-migrate.min.js?ver=1.2.1'></script>
<script type='text/javascript' src='//localhost:8080/wp-content/plugins/gravityforms/js/jquery.json.js?ver=1.9.14'></script>
<script type='text/javascript' src='//localhost:8080/wp-content/plugins/gravityforms/js/gravityforms.min.js?ver=1.9.14'></script>
<script type='text/javascript' src='//localhost:8080/wp-content/plugins/gravityforms/js/conditional_logic.min.js?ver=1.9.14'></script>
What I would like to do is to keep the Gravity Form's scripts, but exclude their inclusion of jQuery and jQuery migrate. I include these myself and do not want GF to do this for me.
I have searched for the handle that includes these scripts, but cannot find the correct hook. I had the following, but it also excluded the GF scripts, which is obviously not what I want to happen:
function remove_unwanted_assets() {
// Gravity Forms plugin.
wp_deregister_script('jquery');
}
add_action('wp_enqueue_scripts', 'remove_unwanted_assets', 100);
This seems to exclude everything from GF.
Any help or guidance is much appreciated. Thanks.
The only way to reliably "deregister" the default jQuery is to keep the priority of your add_action()
call at 10
. This is because Gravity Forms enqueues scripts with a priority of 11
. You'll then need to register your own version of jQuery under the same handle:
function so_33280386_alter_assets() {
// De-register the default version of jQuery
wp_deregister_script( 'jquery' );
// Register a new version of jQuery under the same handle
wp_register_script( 'jquery', 'https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js', false, '1.9.1', true );
}
add_action('wp_enqueue_scripts', 'so_33280386_alter_assets', 10);
I was trying to accomplish the same thing and I found a simple workaround. I am adding jQuery in my own compiled js file so I don't want WordPress's file in there. Simply deregister the script and then re-register it without add a source.
wp_deregister_script( 'jquery' );
wp_register_script( 'jquery', '', '', '', true );
I also am using a few other snippets in order to keep ajax loading loading correctly.
function gravity_js_to_footer() {
return true;
}
add_filter("gform_init_scripts_footer", "gravity_js_to_footer");
// Fix the ajax jquery inline code issue when gravity js is being called in the footer
//https://bjornjohansen.no/load-gravity-forms-js-in-footer
add_filter( 'gform_cdata_open', 'wrap_gform_cdata_open' );
function wrap_gform_cdata_open( $content = '' ) {
$content = 'document.addEventListener( "DOMContentLoaded", function() { ';
return $content;
}
add_filter( 'gform_cdata_close', 'wrap_gform_cdata_close' );
function wrap_gform_cdata_close( $content = '' ) {
$content = ' }, false );';
return $content;
}
With these things combined I am able to not call enqueue jquery and also have everything in the footer.
Good Luck
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With