How to move WordPress core jQuery to footer?

How to move core jquery to footer

Move WordPress core jQuery to footer required when optimizing your website. Otherwise the functioning of a website is doesn’t matter. Normally WordPress core jQuery file located in header section of the website. By using wp_head() function within the header you could be able to call the default jQuery file to the header of the theme.

Now you’ll see how to move default jQuery file to the bottom from header. You just need to use below code in your functions.php. I have seen that most people suggesting and recommending to deregister default jQuery file and re-register and enqueue it in the footer. But the best approach is to use the default jQuery offered from the WordPress repository.

Just digging into the code:

function nerodev_move_jquery_to_footer() { 
 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', 'nerodev_move_jquery_to_footer' );

If you’re using a Third Party Theme

If you’re using a third party theme, you have to create a child theme for the particular parent theme. After creating a child theme you can do the edits which safe during the theme updates. For more information regarding the creating a child theme you can refer this tutorial we published earlier.

By using a Child Theme move WordPress core jQuery to footer

In this case you have to add the above code in the child theme function file. If you add the code in the core theme (parent theme), your code will vanish with a new theme update. So, you have to do all stuff in your child theme. That will keep all your theme changes whether your core theme is getting updated or not.

Hope you’ll enjoy this code snippet to move your jQuery code to the footer in WordPress. That is all from this tutorial regarding move WordPress core jQuery to the footer. Hope you enjoyed this tutorial. Happy learning ahead. Thanks for reading.

Recommended: Please follow our step by step guide how to create a WordPress Theme from scratch. You’ll be able to create your own theme at the end of these lessons.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *