0

Im using successfully the google CDN jQuery files instead of the built in files from Wordpress with the following code in my functions.php:

function register_jquery() {

    if (!is_admin()) {
        wp_deregister_script('jquery-core');
        wp_register_script('jquery-core', 'https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js', true, '2.2.0');
        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' );

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

My Question:

How can i change the code to load the latest jQuery Versions from Google automatically ?

9
  • You should properly indent your code, unfortunately your code is quite messy. Another note, it is not a good idea to enqueue your own copy of jquery. Also, why are you deregitering the scripts and reregistering them Commented Feb 28, 2016 at 14:38
  • I will read the wp docs... I found this code on a other coding site and it works for now. It would be awesome if you can provide me a better solution. Thank you so much
    – NewUser
    Commented Feb 28, 2016 at 14:47
  • It would be awesome if you can provide me a better solution My response and way better solution would be to not use custom scripts loaded from outside of WordPress. Why do you need custom scripts in the first place Commented Feb 28, 2016 at 14:50
  • I think, advantages are the google CDN and the client side caching of the library
    – NewUser
    Commented Feb 28, 2016 at 14:52
  • 2
    The disadvantage to always using latest is when it updates and breaks your site.
    – Milo
    Commented Feb 28, 2016 at 15:47

2 Answers 2

2

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.

1
  • 2
    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 Commented Feb 28, 2016 at 16:09
3

One: Generally, as pointed out in the comments, this is a bad idea for your code, because things will break. https://blog.jquery.com/2014/07/03/dont-use-jquery-latest-js/

Two: There were once links to the "latest" jQuery libraries at the Google API - i.e., using the file name jquery-latest.js - but no more: https://stackoverflow.com/questions/12608242/latest-jquery-version-on-googles-cdn

We know that http://code.jquery.com/jquery-latest.js is abused because of the CDN statistics showing it’s the most popular file. That wouldn’t be the case if it was only being used by developers to make a local copy.

We have decided to stop updating this file, as well as the minified copy, keeping both files at version 1.11.1 forever.

The Google CDN team has joined us in this effort to prevent inadvertent web breakage and no longer updates the file at http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.js. That file will stay locked at version 1.11.1 as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.