How to Move jQuery to Footer in WordPress

By ThemesDNA1 min read

Share:

How to Move jQuery to Footer in WordPress

Loading jQuery in your WordPress website's footer can improve page load times and overall site performance. By default, WordPress loads jQuery in the header, which can delay the rendering of your page. Add the below code to the "functions.php" file of your theme. It will move "jQuery", "jQuery Core", and "jQuery Migrate" loading to the footer of your WordPress site.

function themesdna_move_jquery_to_footer() {
    if (!is_admin()) {
        wp_scripts()->add_data( 'jquery', 'group', 1 );
        wp_scripts()->add_data( 'jquery-core', 'group', 1 );
        wp_scripts()->add_data( 'jquery-migrate', 'group', 1 );
    }
}
add_action('wp_enqueue_scripts', 'themesdna_move_jquery_to_footer');

The above code uses the "wp_scripts()" function to access the global scripts object in WordPress and then uses the "add_data" method to set the "group" property of the "jquery", "jquery-core", and "jquery-migrate" scripts to "1". In WordPress, setting the "group" to "1" indicates that the script should be loaded in the footer. By default, scripts are loaded in the header unless specified otherwise.

Premium WordPress themes Made for WordPress   /   Ready for your story

Ready to Build?

Your next great website starts here.

Build more websites with one flexible collection—and keep every theme for life.

  • 56+ themes
  • Lifetime updates
  • Unlimited websites
  • No renewals

The complete collection

$75

One payment. No renewals.

Get the Themes Bundle — $75