WordPress 5.2.1 deactivated my jQuery

I've been using the code below to use Google's jQuery but the latest WordPress version deactivated it. How can

I've been using the code below to use Google's jQuery but the latest WordPress version deactivated it. How can I use jQuery from Google?

//* Use Google's jQuery
add_action('init', 'use_jquery_from_google');

function use_jquery_from_google () {
    if (is_admin()) {
        return;
    }

    global $wp_scripts;
    if (isset($wp_scripts->registered['jquery']->ver)) {
        $ver = $wp_scripts->registered['jquery']->ver;
    } else {
        $ver = '3.4.0';
    }

    wp_deregister_script('jquery');
    wp_register_script('jquery', "//ajax.googleapis/ajax/libs/jquery/$ver/jquery.min.js", false, $ver);
}

I've been using the code below to use Google's jQuery but the latest WordPress version deactivated it. How can I use jQuery from Google?

//* Use Google's jQuery
add_action('init', 'use_jquery_from_google');

function use_jquery_from_google () {
    if (is_admin()) {
        return;
    }

    global $wp_scripts;
    if (isset($wp_scripts->registered['jquery']->ver)) {
        $ver = $wp_scripts->registered['jquery']->ver;
    } else {
        $ver = '3.4.0';
    }

    wp_deregister_script('jquery');
    wp_register_script('jquery', "//ajax.googleapis/ajax/libs/jquery/$ver/jquery.min.js", false, $ver);
}
Share Improve this question asked May 22, 2019 at 14:18 DesiDesi 1,2494 gold badges37 silver badges54 bronze badges 3
  • 2 Are you sure using the Google CDN is actually faster? – Tom J Nowell Commented May 22, 2019 at 14:21
  • I don't know, to be honest. It was just something that was suggested. Are you saying I shouldn't use the Google CDN? – Desi Commented May 22, 2019 at 14:31
  • @Desi sometimes it's faster, sometimes it isn't, but it's almost universally parrotted as a bonus without anybody ever testing if it improves things. As for 5.2, there's nothing in 5.2 that would stop this from working that I know of. jquery-core lists jquery as a dependent anyway – Tom J Nowell Commented May 22, 2019 at 14:38
Add a comment  | 

1 Answer 1

Reset to default 10

5.2.1 includes a backported fix from jQuery 3.4.0 (commit). Because they're now using a modified version of jQuery they suffixed the version number with '-wp':

$scripts->add( 'jquery', false, array( 'jquery-core', 'jquery-migrate' ), '1.12.4-wp' );

Your code tries to copy the jQuery version number from the existing registration

global $wp_scripts;
if (isset($wp_scripts->registered['jquery']->ver)) {
    $ver = $wp_scripts->registered['jquery']->ver;

The problem is that Google's CDN won't have a version of jQuery called 1.12.4-wp. The following URL

https://ajax.googleapis/ajax/libs/jquery/1.12.4-wp/jquery.min.js

doesn't exist. The same URL without the '-wp' does work. So the error you should be seeing in your browser console is a 404 loading jQuery from the CDN.

The jQuery patch is to fix Trac 47020 which is a security issue in jQuery.extend that could allow cross-site scripting attacks ("Minor vulnerability fix: Object.prototype pollution"). So you probably do want it, either by using WordPress's patched version of 1.2.14 again or by updating to 3.4.0 (if that's compatible with the rest of your site).

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745473085a4629222.html

相关推荐

  • WordPress 5.2.1 deactivated my jQuery

    I've been using the code below to use Google's jQuery but the latest WordPress version deactivated it. How can

    3小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信