2013-11-20 34 views
3

我需要从wordpress中删除jquery ui脚本(以及其他一些)问题是如果我将它们出列并注销它们,那么称为它们的插件不起作用,因为脚本“缺失”,即使我加载了所有的jquery通过googleapis链接使用一个脚本。正确的方法来取代WordPress中排队脚本?

我有以下几点:

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'); 

(其中the- bootstrap对jquery-ui-all有依赖关系)

所有这一切的工作原理是,从谷歌加载jquery-ui并且不再输出单个UI文件 - 问题出现是因为WPFullCalendar插件将其脚本与一堆jQuery UI单独脚本作为依赖关系排队,我猜他们取消注册和出列它的脚本不会得到输出,因为依赖关系丢失?我得到以下错误(由于其JS文件不被输出)

Uncaught ReferenceError: WPFC is not defined 

我怎么能代替脚本中,这样其他的插件仍然可以排队他们还是取决于他们,但他们所有的输出相同的文件。我可以做到这一点,注销所有的名称,然后再以googleapis作为源注册它们,但是我仍然可以获得15个不同的HTTP请求,用于所有相同的googleapi链接...我需要所有这些文件指向1 googleapi文件,只有一个输出。这可能吗? (也不应该出列,把它们从队列中已经调用了其他插件后/取决于他们成功 - wp_print_scripts所有插件后列队发生了,不是吗?)

UPDATE:

我已经将接受的答案修改为更具体一些,以便不会破坏其他任何依赖关系。以下是更新代码,用于正确注销脚本并从任何依赖的脚本中删除它的依赖关系。确保如果你这样做了,你用来替换注销的脚本的脚本在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); 

回答

3

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

现在,除去(所有)脚本的依赖关系如何排队?这个功能应该可以做到。

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); 
+0

谢谢!这里的思考过程是很好的想法,然而,我修改它只是删除我使用'array_diff()'替换的依赖关系。我会将代码添加到问题中,但接受您的答案 - 随时为有相似问题的其他人更新您的答案。 – tsdexter