wp取消注册和wp入队

时间:2011-07-06 13:17:48

标签: jquery wordpress

有人建议我使用wp_unregister和wp_enqueue将gopress jquery库替换为谷歌托管的(因为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');

1 个答案:

答案 0 :(得分:4)

根据我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'

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

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

修改

哦,你的功能也打开了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