Post Pic

How to load jQuery in the footer

By default, WordPress loads the jQuery javascript framework at the top of your files. Here’s a snippet to force WP to load jQuery in the theme footer.

Paste the following code in your functions.php file, save it, and you're done.

function ds_print_jquery_in_footer( &$scripts) {
	if ( ! is_admin() )
		$scripts->add_data( 'jquery', 'group', 1 );
}
add_action( 'wp_default_scripts', 'ds_print_jquery_in_footer' );

Credit: Dominik Schilling.

5 Responses

Oct 14 2012 00:04

The correct way is using`wp_enqueue_scripts’ hook where wp_enqueue_script function has a parameter for footer (boolean header or not).

Oct 29 2012 13:13

Will you tell what is the advantage when loading this jquery in footer ?
thanks in advance,

Oct 30 2012 20:26

An alternative approach would be to disable the jQuery bundled with WordPress using the wp_deregister_script function and let Google host it for you:

ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js

Nov 17 2012 12:05

Hi, You can add this true to the end of the wp_register_script

if( !is_admin()){
wp_deregister_script(‘jquery’);
wp_register_script(‘jquery’, (“http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js”), false, ’1.3.2′, true);
wp_enqueue_script(‘jquery’);
}

Feb 26 2013 11:23

I think it is already been defined in the wp_enqueue_script where we want to load our javascripts files.

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required

WP Theme of the week

Sponsored Likebox