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 One-time payment   /   Lifetime updates

Ready to Build?

Get the ThemesDNA bundle.

Get all premium ThemesDNA themes with lifetime updates and unlimited website usage for a one-time payment.

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

The complete premium theme bundle

$75

One payment. No renewals.

Get the ThemesDNA Bundle — $75