• Is this code correct? Can it be improved further? jquery-migrate.min.js isn’t in Google CDN?

    I also read that it’s possible to automatically check which version WordPress is currently using and download that version from Google CDN.

    Below code seems to work, but of course it will have to be updated manually when WordPress uses different versions.

    function my_init()
    {
    if (!is_admin())
    {
    wp_deregister_script(‘jquery-core’);

    wp_register_script(‘jquery-core’, ‘https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js’, FALSE, ‘1.11.0’, TRUE);

    wp_enqueue_script(‘jquery-core’);

    wp_deregister_script(‘jquery-migrate’);

    wp_register_script(‘jquery-migrate’, ‘/wp-includes/js/jquery/jquery-migrate.min.js’, FALSE, ‘1.2.1’, TRUE);

    wp_enqueue_script(‘jquery-migrate’);
    }
    }
    add_action(‘init’, ‘my_init’);

Viewing 8 replies - 1 through 8 (of 8 total)
  • Moderator James Huff

    (@macmanx)

    You should always only ever use the version of jQuery included with WordPress, as this is what WordPress, and every WordPress plugin and theme, is built on at the time.

    Replacing or modifying the jQuery included with WordPress is essentially just as bad as modifying a core file.

    Thread Starter hlanggo

    (@hlanggo)

    But Google CDN is faster, and I’m not saying that the most recent jquery version will be grabbed from Google CDN.

    I actually got the idea from how WordPress Twenty Fifteen uses Google Fonts, as 2015 grabs the font file directly from Google instead of having the font file hosted locally.

    Moderator James Huff

    (@macmanx)

    Google CDN won’t be faster than loading it locally. Well, ok perhaps it could, but what’s the point of loading jQuery faster than the rest of your site?

    Twenty Fifteen uses Google Fonts most likely because the license on the font it’s using prohibits the WordPress Foundation from distributing the font with the WordPress package.

    Thread Starter hlanggo

    (@hlanggo)

    Not just faster. Google’s version is probably already on the visitor’s computer or mobile device. Meaning it may not have to be loaded at all.

    For mobile users with limited data, that’s a plus.

    Moderator James Huff

    (@macmanx)

    Why would (and how could) any device locally use its own copy of jQuery? That’s just begging for a broken internet experience.

    Really, the disadvantages to using a version of jQuery not included with WordPress (one of those being “everything is broken”) *far* outweigh any perceived advantages.

    Thread Starter hlanggo

    (@hlanggo)

    I didn’t reply 1-2 months ago because the chance for (useless) arguments was getting too high.

    Devices cache especially common scripts used by everything.

    Please See: https://www.ads-software.com/plugins/use-google-libraries/

    Of course, I could just use Google Libraries, but I’m (silly) and also want the scripts to load in footer which GL doesn’t do. Because everyone’s using mobile devices these days, so faster is better.

    Simply put, the idea of taking advantage of cached local copies isn’t silly, especially in countries with poor internet connection.

    If you don’t want to keep manually updating the jQuery versions with each WordPress update:

    $jquery_version = $GLOBALS['wp_scripts']->registered['jquery']->ver;
    
    wp_enqueue_script('jquery','https://ajax.googleapis.com/ajax/libs/jquery/' . $jquery_version . '/jquery.min.js');

    Also works to get jquery-migrate version:

    $jquery_migrate_version = $GLOBALS['wp_scripts']->registered['jquery-migrate']->ver;

    Credit goes to roots: https://github.com/roots/sage

    Hello mdev,

    Could you please provide the full path concerning the credit to your response. Where exactly can it be found at https://github.com/roots/sage ?

    I’m trying to resolve a conflict between /jquery.js?ver=1.11.3 and jquery-migrate.min.js?ver=1.2.1 reported by https://developers.google.com/speed/pagespeed/insights/?url=http%3A%2F%2Fwww.savoiretcroire.ca for my site. I was hoping that reading your source in its context might somehow help me move toward a solution.

    Thank you
    https://www.savoiretcroire.ca

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘jquery.min.js (Google CDN) & jquery-migrate.min.js in footer’ is closed to new replies.