wp_unregister和wp_enqueue [英] wp_unregister and wp_enqueue
问题描述
有人建议我使用wp_unregister和wp_enqueue替换google托管的wordpress jquery库(因为wordpress遇到问题)
it's been suggested to me to use wp_unregister and wp_enqueue to replace wordpress jquery library with a google hosted one (because the wordpress one was having some problems)
但是,当我尝试将其插入我的wordpress网站时,它破坏了我的网站.
However, when i try to insert these into my wordpress site, it breaks my site.
我需要以某种方式包装它们吗?
Do I need to wrap them somehow?
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);
我的站点有一个自定义文件夹,可以在其中放置自定义功能.我尝试失败了
My site has a custom folder where I can put custom functions. I tried this unsuccessfully
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');
推荐答案
基于我编写的代码在此博客文章中进行了展示,该怎么做:
Based on the code I've demonstrated in this blog post, how about this:
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'
我使用了模板重定向挂钩,我想知道这是否是导致您出现问题的原因.
I use the template redirect hook, I wonder if that is what is causing your issues.
无论哪种方法都行不通,您能否提供有关该问题的更多信息,您是否有指向该网站的链接?
Either way if this doesn't work can you give more info on the issue, have you got a link to the site?
修改
哦,您的函数也会在实际上应该已经打开PHP标记的情况下打开它们,请参阅我的评论...
Oh, also your function opens up PHP tags when really they should already be open, see my comments...
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库本身无关.
You'll notice your site is now correctly loading jQuery from Google's CDN, the other script has nothing to do with the jQuery library itself.
这篇关于wp_unregister和wp_enqueue的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!