首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >wp_unregister和wp_enqueue

wp_unregister和wp_enqueue
EN

Stack Overflow用户
提问于 2011-07-06 21:17:48
回答 1查看 3.2K关注 0票数 2

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

然而,当我试图将它们插入到我的wordpress站点中时,它破坏了我的站点。

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

代码语言:javascript
复制
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);

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

代码语言:javascript
复制
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');
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-07-06 21:23:04

根据我使用demonstrated in this blog post编写的代码,如下所示:

代码语言:javascript
复制
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'

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

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

编辑

哦,还有你的函数打开了PHP标签,其实它们应该已经打开了,请看我的评论……

代码语言:javascript
复制
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

你会注意到你的站点现在可以从谷歌的CDN正确加载jQuery了,另一个脚本与jQuery库本身没有任何关系。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6597034

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档