Using Google’s CDN for WordPress JavaScript

As you probably know by now, Google hosts most of the major JavaScript libraries on its own content distribution/delivery network (CDN) for everyone to use. However, WordPress actually comes bundled with many of the same JavaScript libraries. So, what are you to do when you want to use Google’s copy? Sure, you could simply include the call to the Google JavaScript library of your choice in your theme files, but that would cause the library to load twice in many cases (potentially causing conflicts all over the place).

The way to handle this, quite simply, is to tell WordPress not to use its local copy of the library; but to use Google’s copy instead. To do so, you simply “deregister” the WordPress copy (for these examples, I will be showing how to use Google’s jQuery library), then register (and potentially enqueue) the Google copy.

Using Google to Serve Up MooTools or JQuery

You’ll find a lot of tutorials online and various speed-testing tools that tell you to use a content distribution network (CDN) to serve up your images, style sheets and javascript files. When they say that, they’re referring to using cloud storage (a series of servers located across the world). CDN allows you to serve up your content locally to your visitors, making it quicker and easier for them to load your site.

Today, I discovered that Google actually provides a bunch of common javascript libraries, including MooTools, JQuery, Yahoo User Interface (YUI) and more, for free (though, they’ve apparently been doing so for at least a year). Instead of saving the javascript library to your own server and linking to it in that location, you simply link to the javascript files on Google’s server and it’s served up to all of your visitors from their CDN.