Image default
Themes

How to Replace Default WordPress jQuery Script with Google Library

Recently, a user asked us for tips to optimize their WordPress website’s performance. One of our suggestions was to replace the default WordPress jQuery script with jQuery from Google Hosted Libraries.

WordPress includes jQuery with the core software by default. However, we’ve found that replacing it with the latest jQuery version from Google library can improve performance.

In this article, we’ll show you how to easily replace the default WordPress jQuery script with Google library to improve performance and speed.

Why & When You Should Replace Default WordPress jQuery

jQuery is a popular JavaScript library used by developers to create beautiful web applications. It’s used quite often in WordPress to add features like sliders, popups, and much more.

WordPress includes a jQuery library packaged with the core WordPress software. Many of the top WordPress plugins and most popular WordPress themes rely on this library for essential functionality and features.

However, loading jQuery on a shared hosting server can be resource-intensive and slow down your site, especially if you are using any poorly coded WordPress themes or plugins that don’t follow WordPress best practices.

Another issue with using the jQuery version that’s included with WordPress is that it won’t get updated unless WordPress updates it. There could be newer versions of jQuery with performance or security fixes, but you won’t get access to them until they’re included in a WordPress update.

There are a few workarounds to fix this. For instance, you can load jQuery via Google servers or jQuery’s official CDN.

Google Hosted Libraries is a good choice because their servers are stable, updated, and highly optimized for speed which allows jQuery to load much faster on your WordPress site.

That being said, let’s take a look at how to easily replace the default WordPress jQuery with the Google library.

Replace Default WordPress jQuery with Google Library

WordPress comes with a built-in method to easily add scripts and stylesheets. This also allows you to safely remove any scripts or stylesheets that are loaded using the built-in method.

We’ll use this method to first disable the WordPress default jQuery. After that, we’ll tell WordPress to load jQuery via Google library.

You’ll need to add the following code to your theme’s functions.php file, a site-specific plugin, or a code snippets plugin.

function wpb_modify_jquery() {
//check if front-end is being viewed
if (!is_admin()) {
// Remove default WordPress jQuery
wp_deregister_script(‘jquery’);
// Register new jQuery script via Google Library
wp_register_script(‘jquery’, ‘https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js’, false, ‘3.6.0’);
// Enqueue the script
wp_enqueue_script(‘jquery’);
}
}
// Execute the action when WordPress is initialized
add_action(‘init’, ‘wpb_modify_jquery’);

We recommend using the WPCode code snippets plugin to add this code in WordPress. It’s free, easy to use, and won’t break your website if anything goes wrong.

Note: There’s also a premium version of WPCode that offers advanced features like code revisions, automatic conversion pixels, scheduled snippets, a private cloud library, and more.

To get started, you’ll need to install and activate WPCode. If you need help, see our guide on how to install a WordPress plugin.

Once the plugin is activated, navigate to Code Snippets » Add Snippet from your WordPress dashboard. From there, find the ‘Add Your Custom Code (New Snippet)’ option and click the ‘Use snippet’ button underneath it.

From there, you need to select ‘PHP Snippet’ from the list of code types that appear on the screen.

Next, you can add a title for your code snippet. This can be anything to help you remember what the code is for.

After that, simply paste the code from above into the ‘Code Preview’ box.

Lastly, toggle the switch from ‘Inactive’ to ‘Active’ and click on the ‘Save Snippet’ button.

That’s all! WordPress will now replace the default jQuery and load it via Google library instead.

Note: This method will continue loading the jQuery version mentioned in the URL. In the future, you may need to change the Google library URL to use a newer jQuery version.

Google won’t automatically update the version number for you because it could cause compatibility issues if your theme or plugins are relying on a different version.

You can find the latest URL by visiting the Google hosted libraries website.

From there, you can also find the URLs for older versions of jQuery that you can use for troubleshooting if needed.

Replacing Other Default jQuery Libraries

Apart from the core jQuery library, WordPress also includes a bunch of other jQuery scripts. Some of these scripts, like jQuery Mobile and jQuery UI, are hosted on Google library.

For other libraries, you can use jQuery’s own CDN servers to load them much quicker. In the following example, we have replaced the WordPress jquery-ui-core script with the official jquery-ui version.

function wpb_modify_jquery_ui() {
if (!is_admin()) {
wp_deregister_script(‘jquery-ui-core’);
wp_register_script(‘jquery-ui-core’, ‘https://code.jquery.com/ui/1.12.1/jquery-ui.min.js’, false, ‘1.12.1’);
wp_enqueue_script(‘jquery-ui-core’);
}
}
add_action(‘init’, ‘wpb_modify_jquery_ui’);

Just like Google libraries, you will need to replace the URL after a while to use the latest version of the script.

Completely Disable jQuery in WordPress

Due to its wide usage, we don’t recommend completely disabling jQuery on your WordPress website. Even if your WordPress theme doesn’t use jQuery, many popular WordPress plugins still need it.

However, if you’re certain that your website doesn’t need jQuery, then you can safely disable it.

Simply add the following code to your theme’s functions file, a site-specific plugin, or a code snippets plugin like WPCode.

if ( !is_admin() ) wp_deregister_script(‘jquery’);

That’s all. This code simply disables the jQuery script from loading on the front end of your WordPress website.

Troubleshooting jQuery Related Issues in WordPress

New versions of jQuery can sometimes deprecate, or remove, old methods and functions. If a WordPress plugin on your website is using an older method, then this could potentially break things.

You may notice warnings in the Console area of the Inspect tool, or you may notice some features have just stopped working.

WordPress used to use a script called jQuery-migrate to provide backup compatibility with older versions of jQuery. However, since WordPress 5.5 this script has been retired from WordPress.

If you want to add back the jQuery-migrate script or troubleshoot between different jQuery versions, then you can try the following method.

Simply install and activate the Version Control for jQuery plugin. For more details, see our step by step guide on how to install a WordPress plugin.

Upon activation, you need to visit the Settings » jQuery Version Control page. From here, you can choose the jQuery version you want to use.

The plugin will automatically load that version from the official jQuery CDN.

The plugin will also load the jQuery migrate script. If you want, then you can disable it during troubleshooting.

Don’t forget to click on the ‘Save Changes’ button to save your changes.

We hope this article helped you learn how to replace the default WordPress jQuery with Google library. You may also want to see these useful jQuery tutorials for WordPress users or see our complete WordPress performance optimization guide.

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.

Related posts

How to Display Any Number of Posts in a WordPress Loop

admin

19 Best WordPress Starter Themes for Developers in 2025

admin

How to Add Facebook Open Graph Meta Data in WordPress Themes

admin

Leave a Comment