我需要从wordpress中删除jquery脚本(和其他一些脚本)。问题是,如果我去队列并将它们注销,那么调用它们的插件就不能工作了,因为脚本“丢失”了,即使我要用一个脚本加载所有的jquery,并通过googleapis链接加载它们。
我有以下几点:
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并使用以下方法对其进行了排队:
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' );(其中-where带具有jquery all的依赖项)
所有这些都是因为jquery是从google加载的,并且不再输出单独的UI文件,而问题的出现是因为一个插件WPFullCalendar将它的脚本与一堆jquery单独的脚本作为依赖项进行排队,而且我猜与它们一起取消注册和退出队列时,它的脚本不会得到输出,因为缺少依赖项?我得到以下错误(因为它的JS文件没有输出)
Uncaught ReferenceError: WPFC is not defined 如何替换脚本,以便其他插件仍然可以对它们进行排队或依赖它们,但它们都输出相同的文件。我可以这样做,取消注册所有的名字,然后再注册他们作为来源谷歌跳跃,但然后我仍然会得到15个不同的HTTP请求,所有相同的谷歌越界链接.我需要所有这些文件指向1谷歌跳跃文件,并且只有一个文件可以输出。这个是可能的吗?(在其他插件已经成功调用/依赖之后,不应该将它们从队列中删除吗? wp_print_scripts发生在所有插件队列之后,不是吗?)
更新:
我已经修改了下面被接受的答案,使之更加具体,这样就不会破坏其他任何关于依赖关系的内容。下面是正确取消脚本注册并从任何依赖的脚本中删除它的依赖项的更新代码。请确保如果您这样做,用于替换已取消注册的脚本在HTML (可能是head)中加载的足够高,以便以后会有其他依赖它的脚本出现。
//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);发布于 2013-11-20 17:48:10
如果您取消注册脚本,它不会被排队,所以您有几个无用的命令。
现在,删除要排队的(所有)脚本上的依赖项如何?这个函数应该能起作用。
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);https://stackoverflow.com/questions/20101896
复制相似问题