注册Google jQuery被插件覆盖

时间:2012-04-11 作者:Zerpex

我目前正在使用一个插件,其中包括jquery版本1.6.4,如下所示:

function thethe_image_slider_head_scripts(){
    wp_deregister_script( \'jquery\' );
    wp_register_script( \'jquery\', \'https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/  jquery.min.js\', false, \'latest\');
    wp_enqueue_script( \'jquery\' );

    wp_enqueue_script(\'slider-ui\', THETHE_IMAGE_SLIDER_URL.\'style/js/thethe-image-slider.js\', array(\'jquery\'));
}
问题是,我正在函数中注销jquery。我的主题中的php文件,以获取版本1.7.2。但仍然需要1.6.4版。。

我喜欢:

    wp_deregister_script( \'jquery\' );
    wp_register_script(\'jquery\', (get_template_directory_uri() . "/js/jquery-1.7.2.min.js"), false, \'1.7.2\');
    wp_enqueue_script( \'jquery\' );
有人知道为什么它采用1.6.4而不是1.7.2版吗?

谢谢

1 个回复
最合适的回答,由SO网友:Chip Bennett 整理而成

首先:do not de-register core-bundled scripts in order to register some arbitrary version of those scripts.

WordPress目前(在撰写本文时)与jQuery 1.7.1捆绑在一起。当WordPress 3.4下个月发布时,该版本将是(IIRC)1.8。x、 Core、Themes和其他插件都理所当然地希望使用核心捆绑版的jQueryno-conflict mode, 注册任意版本的脚本时,这可能不是真的。因此,您将引入更多潜在的脚本破坏。

第二个:to override a script de-register/enqueue in a Plugin, you need to hook into the same action, and utilize hook priority.

假设插件回调unction thethe_image_slider_head_scripts() {} 正在使用默认优先级,10, 然后您自己的回调需要使用lower 优先级(即ahigher number), 例如11, 像这样:

<?php
function wpse48638_enqueue_scripts() {
    // Theme script enqueues go here
}
add_action( \'wp_enqueue_scripts\', \'wpse48638_enqueue_scripts\', 11 );
?>
或者,您可以从操作中删除插件回调,然后按照常规将自己的脚本排队。注:该remove_action() 调用必须使用same priority 作为插件add_action() 电话:

<?php
remove_action( \'wp_enqueue_scripts\', \'thethe_image_slider_head_scripts\' );
?>
(请注意,在这两种情况下,您都需要引用插件正在使用的相同操作wp_head 甚至init, 而不是wp_enqueue_scripts.)

根据此评论编辑:

让我看看,当我搜索函数时:the\\u image\\u slider\\u head\\u scriptsadd_action(\'wp_print_scripts\', "thethe_image_slider_head_scripts");

这正是我们需要的!

如果您只是想删除插件的脚本排队,只需调用:

<?php
remove_action( \'wp_print_scripts\', \'thethe_image_slider_head_scripts\' );
?>
你必须使用相同的priority 作为add_action() 调用,因此请确保将其保留为默认值(10).

编辑2所以,根据抄本中的这一点智慧,你可能还需要做一件事:

还值得注意的是,您可能需要优先考虑在添加操作后将操作移除到挂钩。在添加操作之前,无法成功删除该操作。

Note: I had to download the Plugin, which required a free "club" membership from the developer, to find this solution. Seeing the full code is imperative to answering these sorts of questions.

这个add_action( \'wp_print_scripts\', \'thethe_image_slider_head_scripts\' ) 函数从另一个回调中调用,该回调本身连接到init. 这就是为什么你remove_action() 调用失败:因为它是在较早的操作中启动的,而您试图删除的操作尚未添加。所以,你需要wrap your remove_action() call inside of a callback also hooked into init, 像这样:

<?php
function wpse48638_init_functions() {
    // Remove TheThe Image Slider scripts
    remove_action( \'wp_print_scripts\', \'thethe_image_slider_head_scripts\' );
}
add_action( \'init\', \'wpse48638_init_functions\' );
?>
还请注意,您可能还需要重新排队\'slider-ui\' 作为要删除的回调的一部分的脚本。

结束

相关推荐

更改WordPress URL会破坏一些jQuery函数

旧URL:http://www.pillerdesigns.com/foxterrier/新建URL:http://www.foxterrier.com/过程:将所有文件复制到新服务器,更新wp config。php,更改了设置中的站点和WordPress URL,使用phpMyAdmin导出DB,然后使用phpMyAdmin导入DB。问题:有三件事在新网站上不再有效,似乎它们都是基于jQuery的:标题中的幻灯片(左侧和右侧图像框)不再过渡,jQuery masonry不再排列薰衣草区帖子,下一代画廊插