admin管理员组文章数量:1426197
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
1 Answer
Reset to default 105.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).
本文标签: WordPress 521 deactivated my jQuery
版权声明:本文标题:WordPress 5.2.1 deactivated my jQuery 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745473084a2659838.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论