Replace The Default WordPress jQuery With Updated Version

Updated 02/06/2024, Posted 02/06/2024 by James Parsons James Parsons 0 Comments

WordPress comes shipped with jQuery; as of today, I believe it's 3.7.1.

If you want to upgrade this to a different version, though, or if you'd prefer to load Google's hosted version that is likely faster loading than your self-hosted version, you can do so with this code by adding it to your functions.php file:

/**
 * Replace the default jQuery with an updated version from Google's CDN.
 */
 
add_action( 'wp_enqueue_scripts', 'replace_default_jquery_with_google_cdn' );
function replace_default_jquery_with_google_cdn() {
    $ver = '3.7.1'; // Update this to change the jQuery version.
    wp_dequeue_script( 'jquery' );
    wp_deregister_script( 'jquery' );
    wp_register_script( 'jquery', 'https://ajax.googleapis.com/ajax/libs/jquery/' . $ver . '/jquery.min.js', false, $ver, true );
    wp_enqueue_script( 'jquery' );
}

Simply replace the version number in this code with the version you'd like to use. This will swap the WordPress jQuery library with Google's super fast-loading version.

Or, if you'd prefer not to use Google-hosted libraries and want to self host, you can use this version:

/**
 * Replace the default jQuery with updated version
 */

add_filter( 'wp_enqueue_scripts', 'replace_default_jquery_with_fallback');
function replace_default_jquery_with_fallback() {
    wp_dequeue_script( 'jquery' );
    wp_deregister_script( 'jquery' );
    wp_register_script( 'jquery', "https://www.yourdomain.com/wp-content/themes/yourtheme/js/jquery.min.js", '', '', false ); // Replace this your jQuery link
    wp_enqueue_script ( 'jquery' );
}

This will dequeue the built-in copy and load your local copy instead. Make sure to swap the example link with your self-hosted version.

Related Code Snippets

Written by James Parsons

Hi, I'm James Parsons! I founded Content Powered, a content marketing agency where I partner with businesses to help them grow through strategic content. With nearly twenty years of SEO and content marketing experience, I've had the joy of helping companies connect with their audiences in meaningful ways. I started my journey by building and growing several successful eCommerce companies solely through content marketing, and I love to share what I've learned along the way. You'll find my thoughts and insights in publications like Search Engine Watch, Search Engine Journal, Forbes, Entrepreneur, and Inc, among others. I've been fortunate to work with wonderful clients ranging from growing businesses to Fortune 500 companies like eBay and Expedia, and helping them shape their content strategies. My focus is on creating optimized content that resonates and converts. I'd love to connect – the best way to contact me is by scheduling a call or by email.