Smart jQuery Inclusion

Smart jQuery Inclusion

Here’s how to add just 4 lines of PHP to your WordPress theme to lock your jQuery version version in place–AND make your site load faster.  It’s a two-fer!

WordPress likes to load a lot of scripts–whether you want it to or not.  jQuery, which is one of those ubiquitous libraries, comes with the WordPress core but it gets updated regularly with each new update.  If you’re running older jQuery plugins that are wanting, say, 1.10, and you update WordPress to a version that comes with jQuery 1.11, strange things can happen. 

Without getting too nerdy, here’s the code to be added to your theme’s functions.php file.

// smart jquery inclusion
if (!is_admin()) {
wp_register_script('jquery', ("//"), false);

Allow me to translate.

Using this smart jQuery inclusion, we first check to see if we’re already logged into the admin.  If not, we proceed.

First, we deregister the jQuery script that ships with WordPress.  We won’t need it anymore.

Then, we register our new script using Google’s hosted CDN of jQuery.  In this example, I’m using Google’s copy of jQuery v1.10.2.  However, Google keeps copies of all previous scripts.  You can get the links to them all from their Hosted Library.  You could just as easily link to jQuery’s own CDN (Thanks MaxCDN!).  All that info is at

Moving on.

The final step is to then enqueue this new jQuery source.

Yeah, you could just add this to the footer.php file like you would with a static site, but then every plugin you add that needs jQuery will enqueue it again.  That means visitors will have to load several different versions of the same script, jQuery, which is silly.

As an added bonus, Google’s hosted libraries are so widely used that most people who have ever visited Google won’t need to download the script at all.  Their browser will simply use the script it downloaded the last time it visited Google (or other site using the same jQuery source).  That adds up to fast, stable WordPress development and that makes us happy.

Leave a Reply