How to Replace the Default WordPress jQuery with a CDN

There are default scripts that comes with each WordPress installation. These scripts are at disposal of theme, and plugin authors to use in their work. From our experience, jQuery is one of the most used scripts in plugins, and themes. You are probably wondering why you should replace the default WordPress jQuery with a CDN or with the one from library like Google Library, and Microsoft Library? jQuery is being used on a lot of sites including the big ones, using jQuery with a CDN, Google Library, and Microsoft Library has become the standards for including scripts in web design. It could be because a CDN, Google Libraries, or Microsoft Libraries are using CDN which is extremely powerful. Because it has now become a standard, a lot of other sites are utilizing it as well, chances are that your blog user has also visited one of those other sites that are using jQuery with a CDN for scripts. So the script is already cached in your user’s browser, and the result will be makes loading of the jQuery script very fast on user’s end. In this topic theme tutorials, we will show you how to replace WordPress jQuery with CDN.

Why You Need Replace WordPress jQuery with CDN

CDN is short for Content Delivery Network which is a network of servers that deliver cached static content from websites to users based on the geographic location of the user. User’s proximity to your web server has an impact on load time. By deploying your content across multiple geographically dispersed servers, you can make your pages load faster from the user’s perspective. This is when CDN comes in handy, in simple words, the closer the CDN server is to where your user is, the faster the user gets the content. That’s it, the advantages if you are replace WordPress jQuery with CDN, then you can clearly see that having a CDN can make a huge impact on your WordPress blog.

How to Replace WordPress jQuery with CDN

Now you are probably wondering, how do I do this? Well its pretty simple, to replace WordPress jQuery with CDN first you need to edit the functions.php, and open the functions.php file of your current WordPress template, and add the following codes below.

Using jQuery’s CDN Provided by Media Temple

All jQuery releases are available on the jQuery CDN, visit jQuery’s CDN page for more information.

if (!is_admin()) add_action("wp_enqueue_scripts", "bloglow_custom_jquery_cdn", 11);
function bloglow_custom_jquery_cdn() {
wp_deregister_script('jquery');
wp_register_script('jquery', "http" . ($_SERVER['SERVER_PORT'] == 443 ? "s" : "") . "://code.jquery.com/jquery-1.9.1.min.js", false, null);
wp_enqueue_script('jquery');
}

Using jQuery’s CDN Provided by Google CDN Library

All jQuery releases are available on the Google CDN Library, visit Google’s CDN page for more information.

if (!is_admin()) add_action("wp_enqueue_scripts", "bloglow_custom_jquery_cdn", 11);
function bloglow_custom_jquery_cdn() {
wp_deregister_script('jquery');
wp_register_script('jquery', "http" . ($_SERVER['SERVER_PORT'] == 443 ? "s" : "") . "://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js", false, null);
wp_enqueue_script('jquery');
}

Using jQuery’s CDN Provided by Microsoft CDN Library

All jQuery releases are available on the Microsoft CDN Library, visit Microsoft’s CDN page for more information.

if (!is_admin()) add_action("wp_enqueue_scripts", "bloglow_custom_jquery_cdn", 11);
function bloglow_custom_jquery_cdn() {
wp_deregister_script('jquery');
wp_register_script('jquery', "http" . ($_SERVER['SERVER_PORT'] == 443 ? "s" : "") . "://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.9.1.min.js", false, null);
wp_enqueue_script('jquery');
}

You are done with this. You now already replace WordPress jQuery with CDN, if you have any questions, or if you have other ideas on how to improve, and add more functionality to the code, be sure to leave a comment!

2 thoughts on “How to Replace the Default WordPress jQuery with a CDN

  1. Is there any drawback from doing this? I know that WordPress changes the version of jquery, so we if we do this, we probably need to manually change/update it whenever WordPress changes the jquery to newer version.

    • Hey,
      There is no drawback from doing this. You right, you need to manually change, or update the jQuery whenever you updated the WordPress, and sometimes no matter what is WordPress version you are using. WordPress is often using the older version. by doing this method you can replace WordPress jQuery with CDN as well with the newer version.

Comments are closed.