Skip to content

Instantly share code, notes, and snippets.

@eriteric
Last active December 27, 2024 20:31
Show Gist options
  • Select an option

  • Save eriteric/5d6ca5969a662339c4b3 to your computer and use it in GitHub Desktop.

Select an option

Save eriteric/5d6ca5969a662339c4b3 to your computer and use it in GitHub Desktop.
Load gravity forms JS in footer
// GF method: http://www.gravityhelp.com/documentation/gravity-forms/extending-gravity-forms/hooks/filters/gform_init_scripts_footer/
add_filter( 'gform_init_scripts_footer', '__return_true' );
// solution to move remaining JS from 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;
}
@JoryHogeveen

JoryHogeveen commented May 11, 2016

Copy link
Copy Markdown

Hi, Found the same code aswell, here is a improvement on the AJAX calls!
EDITED: 2019-09-05

// Force Gravity Forms to init scripts in the footer and ensure that the DOM is loaded before scripts are executed.
add_filter( 'gform_init_scripts_footer', '__return_true' );
add_filter( 'gform_cdata_open', 'wrap_gform_cdata_open', 1 );
add_filter( 'gform_cdata_close', 'wrap_gform_cdata_close', 99 );

function wrap_gform_cdata_open( $content = '' ) {
	if ( ! do_wrap_gform_cdata() ) {
		return $content;
	}
	$content = 'document.addEventListener( "DOMContentLoaded", function() { ' . $content;
	return $content;
}

function wrap_gform_cdata_close( $content = '' ) {
	if ( ! do_wrap_gform_cdata() ) {
		return $content;
	}
	$content .= ' }, false );';
	return $content;
}

function do_wrap_gform_cdata() {
	if (
		is_admin()
		|| ( defined( 'DOING_AJAX' ) && DOING_AJAX )
		|| isset( $_POST['gform_ajax'] )
		|| isset( $_GET['gf_page'] ) // Admin page (eg. form preview).
		|| doing_action( 'wp_footer' )
		|| did_action( 'wp_footer' )
	) {
		return false;
	}
	return true;
}

@consciousimages

Copy link
Copy Markdown

This is nuts we have to do this, thank you both!

@Garth619

Copy link
Copy Markdown

Works great thank you!

@wcandrew

Copy link
Copy Markdown

Not all hero's wear capes. Thank you! I agree it's nuts that we have to do this tho...

@Nicscott01

Nicscott01 commented Nov 16, 2017

Copy link
Copy Markdown

Thanks for this...for some reason its not wrapping my inline script (for AJAX mode) in the added JS.

EDIT: I got it to work. Bad copy/paste/edit when using within my class. Thanks for this!!!

@adampatterson

Copy link
Copy Markdown

@consciousimages This is nuts that we still have to do this. Wasted so much time.

@ericmulder

Copy link
Copy Markdown

Thanks! Saved the day :)

@MelMacaluso

Copy link
Copy Markdown

Thanks ❤️

@noriods

noriods commented Sep 5, 2019

Copy link
Copy Markdown

Thanks @JoryHogeveen! Worked a treat.

@JoryHogeveen

Copy link
Copy Markdown

@noriods
No problem, I forgot I posted this here. Just updated the code to be even more compatible with other parts of WordPress and AJAX.

@noriods

noriods commented Sep 5, 2019

Copy link
Copy Markdown

Nice additions, thanks :)

@alexyoungcl

Copy link
Copy Markdown

thank you for saving me hours of work!

@mikewyattdesign

Copy link
Copy Markdown

Thank you!

@matpassmore

Copy link
Copy Markdown

Thank you. Works a treat!

@sherifkhattab13

Copy link
Copy Markdown

Please tell me where to add those snippets?

@sherifkhattab13

Copy link
Copy Markdown

I added those snippets (one at a time; as they are doing same thing) but on both cases when i defer my javascript on my server, the form doesn't load with an error in console says that jquery is not a function.
Can you please help?

@JoryHogeveen

Copy link
Copy Markdown

@shireefkhatab
My guess is that something in your installation is preventing jQuery from being loaded at all.
In any case, the snipping needs to be placed (as a whole) in your theme functions.php or in a custom plugin. It would also work as a must-use plugin.

@eriteric

eriteric commented May 7, 2020

Copy link
Copy Markdown
Author

Thanks @JoryHogeveen for your contributions.

@danlapteacru

Copy link
Copy Markdown

Hey guys, this not works anymore with GF 2.5, also from GF 2.5 scripts are by default in footer.

@jesserosenfield

jesserosenfield commented May 11, 2021

Copy link
Copy Markdown

This breaks for me when using conditional logic

Uncaught ReferenceError: gf_global is not defined

Error is thrown from this line:

gf_global["number_formats"][5] = {"1":{"price":false,"value":false},"2":{"price":false,"value":false},"3":{"price":false,"value":false},"4":{"price":false,"value":false},"13":{"price":false,"value":false},"12":{"price":false,"value":false},"6":{"price":false,"value":false},"7":{"price":false,"value":false},"8":{"price":false,"value":false},"10":{"price":false,"value":false}};if(window['jQuery']){if(!window['gf_form_conditional_logic'])window['gf_form_conditional_logic'] = new Array();window['gf_form_conditional_logic'][5] = { logic: { 10: {"field":{"actionType":"show","logicType":"all","rules":[{"fieldId":"1","operator":"is","value":"River Club Apartments"}]},

@TeemuSuoranta

Copy link
Copy Markdown

From GF 2.5 this breaks the forms (that have coniditional logic). See https://community.gravityforms.com/t/gf-2-5-7-5-is-broken/10323/8

I'd advice not using this any more and removing it anywhere where it is added.

@JoryHogeveen

Copy link
Copy Markdown

Correct, this code is redundant since GF 2.5+

@BadScooter1980

Copy link
Copy Markdown

I know this is old, but I'm struggling with GF placing the js in the header. Not only that, but it's moving my enqueued jQuery back into the header, too. Any ideas?

@JoryHogeveen

Copy link
Copy Markdown

Hi @BadScooter1980
This is old, redundant and therefore deprecated code, please do not use anymore in newer GF versions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment