Always the Latest google jQuery instead of default WordPress jQuery

The Good Answer

The only good answer to this question is don’t do it – simple as that.

Software in general expect things to work certain ways and libraries that are constantly evolving can have breaking changes. That means the change will cause another piece of software to stop working correctly which may be limited in scope or break all JS on the page.


Google CDN does not have directory for latest.

versions:

2.2.0, 2.1.4, 2.1.3, 2.1.1, 2.1.0, 2.0.3, 2.0.2, 2.0.1, 2.0.0, 1.12.0, 1.11.3, 1.11.2, 1.11.1, 1.11.0, 1.10.2, 1.10.1, 1.10.0, 1.9.1, 1.9.0, 1.8.3, 1.8.2, 1.8.1, 1.8.0, 1.7.2, 1.7.1, 1.7.0, 1.6.4, 1.6.3, 1.6.2, 1.6.1, 1.6.0, 1.5.2, 1.5.1, 1.5.0, 1.4.4, 1.4.3, 1.4.2, 1.4.1, 1.4.0, 1.3.2, 1.3.1, 1.3.0, 1.2.6, 1.2.3

But you can reference the jQuery CDN – https://code.jquery.com/jquery-latest.min.js – although it is highly unadvised by jQuery based on their post – Don’t Use jquery-latest.js

Earlier this week the jQuery CDN had an issue that made the jquery-latest.js and jquery-latest.min.js files unavailable for a few hours in some geographical areas. (This wasn’t a problem with the CDN itself, but with the repository that provides files for the CDN.) While we always hope to have 100% uptime, this particular outage emphasized the number of production sites following the antipattern of using this file. So let’s be clear: Don’t use jquery-latest.js on a production site.

The Bad Answer

While everyone you talk to should tell you never to use latest in production, when you decide to throw caution to the wind just be sure to postfix your methods with yolo. This ensures that anyone looking at your code immediately recognizes that you’re a rebel and sometimes you gotta break your site to make some omelettes.

Unfortunately, or fortunately depending on who you talk to, the rest of the libraries can’t be referenced by latest.

function register_jquery_yolo() {

    // FIXME: This conditional should be removed:
    // if ( ! is_admin() ) {  

        wp_deregister_script( 'jquery-core' );
        wp_register_script( 'jquery-core', 'https://code.jquery.com/jquery-latest.min.js', true, 'latest-yolo' );
        wp_enqueue_script( 'jquery-core' );

        wp_deregister_script( 'jquery-migrate' );
        wp_register_script( 'jquery-migrate', 'https://cdn.jsdelivr.net/jquery.migrate/1.2.1/jquery-migrate.min.js', true, '1.2.1' );
        wp_enqueue_script( 'jquery-migrate' );

        wp_deregister_script( 'jquery-ui' );
        wp_register_script( 'jquery-ui', 'https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js', true, '1.11.4' );
        wp_enqueue_script( 'jquery-ui' );
    // }
}

add_action( 'wp_enqueue_scripts', 'register_jquery_yolo' );

I think, advantages for using the google lib are the google CDN and the client side caching of the library.

One BIG problem with using latest is that you don’t actually want to cache the file because the url always needs to point to an up-to-date file. How else would you get the latest if it was cached?


I still can’t understand why anyone still uses if ( ! is_admin() ) when using wp_enqueue_scripts. It is totally useless with no purpose at all – Pieter Goosen

As was pointed out if ( ! is_admin() ) should not be used in this case. wp_enqueue_scripts is for front-end scripts, admin_enqueue_scripts for admin scripts, and login_enqueue_scripts for login pages.