jquery - wp_unregister 和 wp_enqueue

标签 jquery wordpress

有人建议我使用 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/

相关文章:

javascript - 使用 jQuery Stopwatch 将毫秒从计时器传递到表单提交的输入

php - 在 JOINED MySQL 查询中过滤 MAX() 函数

php - 尝试在Wordpress的自定义头像功能中获取非对象通知的属性

php - 在 WooCommerce 中以编程方式创建可变产品和两个新属性

php - wordpress posts_orderby 过滤器与插件中的自定义表

javascript - 将URL中括号内的字符串转换为查询参数: Javascript

javascript - ajax jQuery 中的 getAttribute()

jQuery UI Accordion 与滑动条冲突

jquery - 限制jquery ui可拖动外框边框的移动

php - 如何使顶部导航中的单个链接在 wordpress 中具有不同的背景颜色?