How to Load jQuery from the Google CDN Using wp_register_script in WordPress

Home » Snippets » How to Load jQuery from the Google CDN Using wp_register_script in WordPress
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

In the world of web development, efficiency is key. And when it comes to WordPress, loading JQuery from the Google CDN (Content Delivery Network) using wp_register_script can significantly speed up your website’s load time and overall performance. This specific method allows you to leverage Google’s powerful infrastructure, resulting in faster, more reliable loading of JQuery. It’s a more technical process, but don’t worry – in this article, we’ll guide you step-by-step on how to do this effectively.

					function wpturbo_load_google_jquery() {
    if (!is_admin()) {
        wp_deregister_script('jquery');
        wp_register_script('jquery', 'https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js', array(), null, true);
        wp_enqueue_script('jquery');
    }
}
add_action('wp_enqueue_scripts', 'wpturbo_load_google_jquery');
				

The function wpturbo_load_google_jquery is first initialized in your code. This function is important as it will be modifying which source WordPress uses to load jQuery.

Within this function, the conditional if (!is_admin()) checks to see if the current user is an admin. If not, it proceeds with the following set of operations which target the ‘jquery’ script registration within WordPress.

First, the wp_deregister_script('jquery') line is run. This uses a built-in WordPress function which deregisters, or in other words, deactivates, the current ‘jquery’ script registration. This provides a fresh slate for registering a new ‘jquery’ script source.

Next, the wp_register_script function is used. This function is designed to register a new script source. In this instance, it is being used to register a jQuery script.

The parameters of the function include the script’s handle (‘jquery’), the source in the form of an URL (‘https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js‘), an empty dependencies array and the version as null. The last parameter sets whether the script should be placed in the footer of the HTML document, in this case, it’s set to true.

After the script has been deregistered and then newly registered, the wp_enqueue_script('jquery') statement is run. This instructs WordPress to add jQuery to the queue of scripts that will be loaded on the site’s front-end.

To make this function to take effect, it has to be hooked to an action, in this case, the ‘wp_enqueue_scripts’ action. The add_action function does this, linking the ‘wp_enqueue_scripts’ event to our function wpturbo_load_google_jquery.

This results in the JavaScript file being loaded not from WordPress but, from Google’s CDN (Content Delivery Network). Utilizing a CDN for jQuery can increase the load speed of your website, as well as provide some level of fail-safe if for some reason the resource fails to load from the original server.

Register an account to save your snippets or go Pro to get more features.