Categories
Wordpress

Defer loading of JavaScript in WordPress

Deferring loading of JavaScript functions that are not called at startup reduces the initial download size, allowing other resources to be downloaded in parallel, and speeding up execution and rendering time.

While this “lazy” method of loading doesn’t reduce the total JS payload, it can significantly reduce the number of bytes needed to load the initial state of the page, and allows the remaining bytes to be loaded asynchronously in the background.

In order to speed up the loading of a page all the JavaScript in the HTML header has to be run after the page finish to load, more precisely at the end of the page before the </body> tag.

WordPress automatically adds all the core and plugin JavaScript in the HTML header using the function wp_head() or at the end of the page using wp_footer().

All the JavaScript contained in the header wp_head() has to be moved at the end of the page in wp_footer().

In order to do that you can add those simple line in your function.php of the your theme.

if(!is_admin()) {
	// Move all JS from header to footer
	remove_action('wp_head', 'wp_print_scripts');
	remove_action('wp_head', 'wp_print_head_scripts', 9);
	remove_action('wp_head', 'wp_enqueue_scripts', 1);
	add_action('wp_footer', 'wp_print_scripts', 5);
	add_action('wp_footer', 'wp_enqueue_scripts', 5);
	add_action('wp_footer', 'wp_print_head_scripts', 5);
}

All this will be valid just in the public website not for the admin panel.

Make sure that in your theme you will not add any other JavaScript in your header.

If you use HTML5, Modernizr (or html5shiv) needs to be in the HTML header, in order to make the old browsers (i.e. IE7, IE8) HTML5 and CSS3 friendly.