2014-05-07 35 views
11

我有一個兒童主題,我可以添加我想用來替換一些主題功能和替換一些按鈕的腳本。wp_dequeue_script爲兒童主題取代腳本

但我無法刪除舊按鈕,因此它們都顯示在對方之上。我如何刪除父級js腳本?

這裏是我function.php的兒童主題

function replace_scroll(){ 
    // Remove the Default JavaScript  
    wp_dequeue_script('dp-js'); 

    // Add your own script 
    $js_url = get_bloginfo('stylesheet_directory') . '/js'; 
    wp_enqueue_script('dp',"$js_url/dp1.scripts.js"); 
} 
add_action('wp_enqueue_scripts', 'replace_scroll'); 

回答

13

這裏的幾個問題與

  • 開始你應該使用get_stylesheet_directory_uri()兒童主題和get_template_directory_uri()父主題,而不是get_bloginfo()功能。後者是慢,使用前兩個功能

  • 腳本和樣式應註銷和出列從隊列中完全刪除它們

  • 優先級是很重要的。您需要稍後掛鉤您的功能,以確保在註銷樣式和腳本之前註冊它們,否則它將無法工作。

解決方案:

複製父js文件到你的子主題,並打開它,並作出必要的改變。保存文件

現在你需要離隊註銷父js文件,然後排隊你新的子主題的js文件

/* 
* Use any number above 10 for priority as the default is 10 
* any number after 10 will load after 
*/ 
add_action('wp_enqueue_scripts', 'my_custom_scripts', 100); 
function my_custom_scripts() 
{ 
    wp_dequeue_script('parent-script-handle'); 
    wp_deregister_script('parent-script-handle'); 
    // Now the parent script is completely removed 

    /* 
    * Now enqueue you child js file, no need to register if you are not 
    * doing conditional loading 
    */ 
    wp_enqueue_script('child-script-handle', get_stylesheet_directory_uri() . '/child-script.js'); 
    //Now we have done it correctly 
} 
+1

感謝您提供其他信息。儘管使用PHP_INT_MAX似乎很漂亮OTT? –

+0

可能是的,但我喜歡在兒童主題中使用它,以確保我的功能最後被鎖定。這完全是你自己的偏好 –

+0

讓這個工作起作用的關鍵是確保你匹配你的父母主題中的'add_action'之後正在使用的內容。有時使用'wp_enqueue_scripts',有時使用'wp_footer',這取決於文件和主題。 – fidev

4

你需要找出其中父主題生成按鈕。

如果它們是由javascript生成的,則可以使正在創建按鈕的腳本出列。只要小心,因爲該文件中的任何其他JavaScript也將被刪除。如果這是一個問題,最好的方法是將js腳本複製到主題文件夾中,刪除不需要的部分,將原始隊列出隊,然後排隊更改後的腳本。

如何做到這一點。

要將腳本出隊,您需要知道它是否處理。 Here's關於如何獲得腳本句柄的絕妙教程,非常方便。爲了以防總結鏈路出現死:

添加到您的functions.php文件

function wpcustom_inspect_scripts_and_styles() { 
    global $wp_scripts; 
    global $wp_styles; 

    // Runs through the queue scripts 
    foreach($wp_scripts->queue as $handle) : 
     $scripts_list .= $handle . ' | '; 
    endforeach; 

    // Runs through the queue styles 
    foreach($wp_styles->queue as $handle) : 
     $styles_list .= $handle . ' | '; 
    endforeach; 

    printf('Scripts: %1$s Styles: %2$s', 
    $scripts_list, 
    $styles_list); 
} 

add_action('wp_print_scripts', 'wpcustom_inspect_scripts_and_styles'); 

這將打印所有JS的列表和CSS加載到頁面頂部。如果你在解決問題時遇到困難,你可以隨時使用dom檢查器,例如Chrome或Firefox。在鉻 - 右鍵單擊​​ - 檢查元素 - 資源 - 框架 - 您的網站網址 - 腳本。

這會給你一個所有腳本及其位置的列表。

一旦你找出了你需要的腳本,將它複製到你的主題並刪除添加按鈕的部分。

然後使用我們之前找到的句柄使不需要的腳本出列,並排入新的腳本。

function wpcustom_deregister_scripts_and_styles(){ 

    wp_deregister_script('my-script-handle'); 
    wp_enqueue_script('my-new-script', get_template_directory_uri() . '/js/my-new-script.js', array(), '1.0', true); 
} 

add_action('wp_print_styles', 'wpcustom_deregister_scripts_and_styles', 100); 

如果正在由PHP生成的按鈕,你需要找到正在生成您的父級主題的HTML文件,將它複製到你的子主題,並刪除這些行。

8

測試使用具有這種在其functions.php文件父主題二十十四:

function twentyfourteen_scripts() { 
    // other enqueues 
    wp_enqueue_script('twentyfourteen-script', get_template_directory_uri() . '/js/functions.js', array('jquery'), '20131209', true); 
} 
add_action('wp_enqueue_scripts', 'twentyfourteen_scripts'); 

行動wp_enqueue_scripts沒有宣佈的優先級,所以它使用默認10。 爲了讓我們有離隊的工作在我們兒童主題添加一個低優先級functions.php文件:

function remove_twentyfourteen_scripts() 
{ 
    wp_dequeue_script('twentyfourteen-script'); 
} 
add_action('wp_enqueue_scripts', 'remove_twentyfourteen_scripts', 11); // <-- HERE 
+0

這對我有用。非常感謝! –

+4

請注意,「較低」的優先級意味着該動作需要稍後觸發,因此add_action的優先級參數需要更大,令人困惑的是吧?優先級較低的參數意味着它早先觸發,更高意味着它稍後觸發 – edhurtig

3

我想補充一點,我們需要使用get_stylesheet_directory_uri()添加替換腳本時,因爲get_template_directory_uri()返回父主題目錄。

查看WordPress的文檔here

3

在典型的WordPress的流動,插件是加載在主題之前,並且在子主題的情況下,在父主題之前加載子主題,具體而言,functions.php子主題的文件被包括在父類的functions.php之前。因此,wp_enqueue_scripts動作堆疊起來,然後按照該順序執行,除非動作的優先級被定義爲不同於默認值(10)。這裏的問題是,你正試圖將尚未入隊的腳本解除隊列。


,以消除家長添加腳本,提高兒童主題的wp_enqueue_scripts行動的優先重視比父主題行動定義值。

// get_template_directory_uri(), if used in child theme, the parent theme directory URI will be returned, 
// use get_stylesheet_directory_uri() to get the child's theme directory uri 

add_action('wp_enqueue_scripts', 'child_theme_enqueue_scripts', 11); 
function child_theme_enqueue_scripts() 
{ 
    wp_dequeue_script('parent-theme-script'); 
    wp_enqueue_script('child-theme-script', get_stylesheet_directory_uri() . '/js/script.js'); 
} 


要更換的腳本,例如,你想做出一些改變,並保留現有的依賴關係和定位有兩種方式。

使用wp_enqueue_script具有相同handle名字,因爲事實上,如果您嘗試註冊或排隊已註冊的手柄,新的參數會被忽略,有用的,如果劇本後來被父母本地化,以保持局部數據。

// default priority 10 

add_action('wp_enqueue_scripts', 'child_theme_enqueue_scripts'); 
function child_theme_enqueue_scripts() 
{ 
    wp_enqueue_script('parent-theme-script', get_stylesheet_directory_uri() . '/js/script.js'); 
} 

使用全局$wp_scripts對象只修改所需的屬性,例如,只改變src已經排隊或註冊的腳本的屬性。

// priority 11 

add_action('wp_enqueue_scripts', 'child_theme_enqueue_scripts', 11); 
function child_theme_enqueue_scripts() 
{ 
    global $wp_scripts; 
    $wp_scripts->registered[ 'parent-theme-script' ]->src = get_stylesheet_directory_uri() . '/js/script.js'; 
} 


該子主題被加載第一,這個事實很實用,因爲使用童工的主題並不是要取代,但更多的是除了主旋律。