gpt4 book ai didi

php - 在 wordpress 中替换排队脚本的正确方法?

转载 作者:搜寻专家 更新时间:2023-10-31 20:41:55 25 4
gpt4 key购买 nike

我需要从 wordpress 中删除 jquery ui 脚本(和其他一些脚本)问题是如果我将它们出列并取消注册,那么调用它们的插件将无法工作,因为脚本“丢失”,即使我正在加载所有通过 googleapis 链接使用一个脚本的 jquery ui。

我有以下内容:

add_action('wp_print_scripts','remove_excess_scripts', 100);
function remove_excess_scripts(){
wp_dequeue_script( 'jquery-ui-core' );
wp_dequeue_script( 'jquery-ui-widget' );
wp_dequeue_script( 'jquery-ui-mouse' );
wp_dequeue_script( 'jquery-ui-accordion' );
wp_dequeue_script( 'jquery-ui-autocomplete' );
wp_dequeue_script( 'jquery-ui-slider' );
wp_dequeue_script( 'jquery-ui-progressbar' );
wp_dequeue_script( 'jquery-ui-tabs' );
wp_dequeue_script( 'jquery-ui-sortable' );
wp_dequeue_script( 'jquery-ui-draggable' );
wp_dequeue_script( 'jquery-ui-droppable' );
wp_dequeue_script( 'jquery-ui-selectable' );
wp_dequeue_script( 'jquery-ui-position' );
wp_dequeue_script( 'jquery-ui-datepicker' );
wp_dequeue_script( 'jquery-ui-tooltip' );
wp_dequeue_script( 'jquery-ui-resizable' );
wp_dequeue_script( 'jquery-ui-dialog' );
wp_dequeue_script( 'jquery-ui-button' );

wp_deregister_script( 'jquery-ui-core' );
wp_deregister_script( 'jquery-ui-widget' );
wp_deregister_script( 'jquery-ui-mouse' );
wp_deregister_script( 'jquery-ui-accordion' );
wp_deregister_script( 'jquery-ui-autocomplete' );
wp_deregister_script( 'jquery-ui-slider' );
wp_deregister_script( 'jquery-ui-progressbar' );
wp_deregister_script( 'jquery-ui-tabs' );
wp_deregister_script( 'jquery-ui-sortable' );
wp_deregister_script( 'jquery-ui-draggable' );
wp_deregister_script( 'jquery-ui-droppable' );
wp_deregister_script( 'jquery-ui-selectable' );
wp_deregister_script( 'jquery-ui-position' );
wp_deregister_script( 'jquery-ui-datepicker' );
wp_deregister_script( 'jquery-ui-tooltip' );
wp_deregister_script( 'jquery-ui-resizable' );
wp_deregister_script( 'jquery-ui-dialog' );
wp_deregister_script( 'jquery-ui-button' );
}

加上前面的函数,我正确地注册了 jquery ui 并将其排入队列:

wp_register_script(
'jquery-ui-all',
"http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js",
array('jquery'),
false,
false
);

function print_scripts() {
wp_enqueue_script( 'the-bootstrap' );
}
add_action( 'wp_enqueue_scripts', 'print_scripts' );

(其中 the-bootstrap 依赖于 jquery-ui-all)

所有这些都是从 jquery-ui 从 google 加载并且不再输出任何单独的 UI 文件的意义上工作的 - 问题出现是因为插件 WPFullCalendar 将其脚本与一堆 jquery ui 单独的脚本作为依赖项排队我猜他们注销并出队了,因为缺少依赖项,它的脚本没有得到输出?我得到以下错误(由于它的 JS 文件没有被输出)

Uncaught ReferenceError: WPFC is not defined 

我如何替换脚本,以便其他插件仍然可以将它们排入队列或依赖于它们,但它们都输出相同的文件。我可以这样做是注销所有名称,然后使用 googleapis 作为源再次注册它们,但是我仍然会收到 15 个不同的 HTTP 请求,用于所有相同的 googleapi 链接...我需要所有这些文件指向 1 googleapi 文件,并且只为一个输出。这可能吗? (并且不应该在其他插件已经成功调用/依赖它们之后将它们从队列中取出 - wp_print_scripts 发生在所有插件入队之后,不是吗?)

更新:

我已经将下面接受的答案修改得更具体一些,以免在依赖性方面破坏其他任何东西。这是正确注销脚本并从任何依赖的脚本中删除它的依赖项的更新代码。确保如果你这样做,你用来替换注销脚本的脚本在 HTML 中加载得足够高(可能是头部),以便依赖它的其他脚本随后出现。

//custom deregister scripts
function georgian_deregister_scripts(){
global $wp_scripts;
//scripts to deregister
$deregisteredscripts = array('jquery-ui-core','jquery-ui-widget','jquery-ui-mouse','jquery-ui-accordion','jquery-ui-autocomplete','jquery-ui-slider','jquery-ui-progressbar' ,'jquery-ui-tabs','jquery-ui-sortable','jquery-ui-draggable','jquery-ui-droppable','jquery-ui-selectable','jquery-ui-position','jquery-ui-datepicker','jquery-ui-tooltip','jquery-ui-resizable','jquery-ui-dialog','jquery-ui-button');
//degregister each script
foreach($deregisteredscripts as $script)
wp_deregister_script( $script );
//remove deregistered scripts as dependencies of any other scripts depending on them
if(false != $wp_scripts->queue)
foreach($wp_scripts->queue as $script)
if(isset($wp_scripts->registered[$script]))
$wp_scripts->registered[$script]->deps = array_diff($wp_scripts->registered[$script]->deps, $deregisteredscripts);
}
add_action('wp_enqueue_scripts', 'georgian_deregister_scripts', 101);

最佳答案

如果您注销脚本,它不会入队,所以您有几个无用的命令。

现在,如何删除对(所有)要排队的脚本的依赖?这个函数应该可以解决问题。

function customize_scripts_output(){
global $wp_scripts;

if(false != $wp_scripts->queue)
foreach($wp_scripts->queue as $script)
if(isset($wp_scripts->registered[$script]))
$wp_scripts->registered[$script]->deps = array();
}
add_action('wp_enqueue_scripts', 'customize_scripts_output', 101);

关于php - 在 wordpress 中替换排队脚本的正确方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20101896/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com