2013-11-20 35 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