gpt4 book ai didi

jquery - wp_unregister 和 wp_enqueue

转载 作者:行者123 更新时间:2023-12-01 03:24:39 24 4
gpt4 key购买 nike

有人建议我使用 wp_unregister 和 wp_enqueue 将 wordpress jquery 库替换为 google 托管的库(因为 wordpress 有一些问题)

但是,当我尝试将这些插入我的 WordPress 网站时,它破坏了我的网站。

我需要以某种方式包裹它们吗?

wp_unregister_script('jquery');
wp_unregister_script('jquery-ui-core');

wp_enqueue_script('jquery', https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js);
wp_enqueue_script('jquery-ui-core', https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.14/jquery-ui.min.js);

我的网站有一个自定义文件夹,我可以在其中放置自定义函数。我尝试过,但没有成功

function googlejqueryhost(){


<?php
wp_unregister_script('jquery');
wp_unregister_script('jquery-ui-core');

wp_enqueue_script('jquery', https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js);
wp_enqueue_script('jquery-ui-core', https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.14/jquery-ui.min.js);


?>
}
add_action('wp_head', 'googlejqueryhost');

最佳答案

基于我的代码 demonstrated in this blog post ,这个怎么样:

function load_jquery() {

// only use this method is we're not in wp-admin
if (!is_admin()) {

// deregister the original version of jQuery
wp_deregister_script('jquery');

// discover the correct protocol to use
$protocol='http:';
if($_SERVER['HTTPS']=='on') {
$protocol='https:';
}

// register the Google CDN version
wp_register_script('jquery', $protocol.'//ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js', false, '1.5.2');

// add it back into the queue
wp_enqueue_script('jquery');

}

}

add_action('template_redirect', 'load_jquery'

我使用模板重定向 Hook ,我想知道这是否是导致您出现问题的原因。

无论哪种方式,如果这不起作用,您能否提供有关该问题的更多信息,您有该网站的链接吗?

编辑

哦,你的函数还打开了 PHP 标签,而实际上它们应该已经打开了,请参阅我的评论...

function googlejqueryhost(){


<?php // HERE, remove this
wp_unregister_script('jquery');
wp_unregister_script('jquery-ui-core');

wp_enqueue_script('jquery', https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js);
wp_enqueue_script('jquery-ui-core', https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.14/jquery-ui.min.js);


?> // AND HERE, remove this
}

编辑2

您会注意到您的网站现在可以从 Google 的 CDN 正确加载 jQuery,其他脚本与 jQuery 库本身无关。

enter image description here

关于jquery - wp_unregister 和 wp_enqueue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6597034/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com