WordPress 引入 css/js 方法很多,條件很多。如何全局加載,或僅在某些頁面精準加載,什麼時候需要先註冊腳本再加載,本文希望找到最簡單的方式,並給出探索更多方法的途徑。

在前台加載 css/js
用 wp_enqueue_script() 函數加載 js,用 wp_enqueue_style() 加載 css,加載資源的位置 (action) 只有一個——wp_enqueue_scripts 。

用 wp_enqueue_系列函數可以更好的處理腳本樣式表的依賴關係,防止重複加載,以 twentyfifteen 主題為例。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

functiontwentyfifteen_scripts(){

    //全局加載一般的樣式表

    wp_enqueue_style('genericons',get_template_directory_uri().'/genericons/genericons.css',array(),'3.2');

    //全局加載主樣式表

    wp_enqueue_style('twentyfifteen-style',get_stylesheet_uri());

    //全局加載僅用於 IE 的樣式表

    wp_enqueue_style('twentyfifteen-ie',get_template_directory_uri().'/css/ie.css',array('twentyfifteen-style'),'20141010');

    wp_style_add_data('twentyfifteen-ie','conditional','lt IE 9');

    //全局加載 js 腳本

    wp_enqueue_script('twentyfifteen-script',get_template_directory_uri().'/js/functions.js',array('jquery'),'20141212',true);

    //給 js 腳本傳遞變量,解決腳本中不能調用 php 的問題

    wp_localize_script('twentyfifteen-script','screenReaderText',array(

        'expand'   =>'<span >'.__('expand child menu','twentyfifteen').'</span>',

        'collapse'=>'<span >'.__('collapse child menu','twentyfifteen').'</span>',

    ));

}

add_action('wp_enqueue_scripts','twentyfifteen_scripts');

若僅在某些頁面加載,利用 WordPress 的 Conditional Tags 即可。

什麼時需要先註冊 css/js
即何時需要使用 wp_register_script() 和 wp_register_style() 函數。

當 css/js 很多,並且要分情況加載時,使用 wp_register_script() 可以更好的管理資源,避免重複勞動。下面的示例代碼中,先在 init action 上把所有需要用到樣式表都註冊一遍,之後不管想在哪裏引入,都可以簡單的用 wp_enqueue_style( $handle ) 來加載。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

// 在 init action 處註冊腳本,可以與其它邏輯代碼放在一起

functionmy_init(){

    $url=get_template_directory_uri();

    // 註冊樣式表

    $styles=array(

        'style1'=>$url.'/css/style1.css',

        'style2'=>$url.'/css/style2.css',

        'style3'=>$url.'/css/style3.css'

    );

    foreach($styles as$k=>$v){

        wp_register_style($k,$v,false);

    }

    // 註冊腳本

    // 其它需要在 init action 處運行的腳本

}

add_action('init','my_init');

註冊腳本時需要運行 $wp_scripts->add( $handle, $src, $deps, $ver );,若腳本沒有註冊直接使用 wp_enqueue_script,需要先調用 add 方法,也就是説重複 enqueue 一個腳本就會運行多次 add 方法,降低了程序的效率。

在 WordPress 登錄頁面加載
將 action 替換為 login_enqueue_scripts 即可,例如

functionenqueue_for_login(){

    wp_enqueue_style('core','style.css',false);

    wp_enqueue_script('my-js','filename.js',false);

}

add_action('login_enqueue_scripts','enqueue_for_login');

如果想了解其它方式,可以仔細閲讀 wp-login.php 。

在後台全局加載
同理,將 action 改為 admin_enqueue_scripts

functionmy_enqueue(){

    wp_enqueue_script('my_custom_script',plugin_dir_url(__FILE__).'myscript.js');

}

add_action('admin_enqueue_scripts','my_enqueue');

想了解更多方法,請閲讀 wp-admin/admin-header.php 。

在後台按需加載
僅用於後台某些頁面的資源只在這些頁面加載就好,不要到處使用,可以減少不必要的衝突。

1. $hook_suffix

首先我們可以根據 admin_enqueue_scripts 這個 action 傳遞的 $hook_suffix 參數來判斷所處的頁面,例如僅在 edit.php 加載,代碼如下

functionmy_enqueue($hook_suffix){

    if('edit.php'==$hook_suffix){

       wp_enqueue_script('my_custom_script',plugin_dir_url(__FILE__).'myscript.js');

    }  

}

add_action('admin_enqueue_scripts','my_enqueue');

edit.php 就是 post 、 page 或者 custom post type 的列表頁面,編輯頁面是 post.php,新建頁面是 post-new.php,可以在不同頁面打印 $hook_suffix 來瞭解它的使用方法。但由此也可看出它不能區分現在是在哪種 post 頁面,需要藉助更多的全局變量來判斷。

2. $typenow

全局變量 $typenow 可以告訴我們當前的 post type,例如僅在 post 的列表頁面加載可以這樣來判斷

functionmy_enqueue($hook_suffix){

    global$typenow;

    if('edit.php'==$hook_suffix&&$typenow=='post'){

       wp_enqueue_script('my_custom_script',plugin_dir_url(__FILE__).'myscript.js');

    }  

}

add_action('admin_enqueue_scripts','my_enqueue');

3. get_current_screen()

上述兩個全局變量可以區分大多數情況,若區分不了,可以試試使用 get_current_screen() 函數,該函數返回當前頁面的 post type 、 ID 、 base 等信息,只能在 admin_init 之後使用,具體可以參考官方文檔。

4. $pagenow

全局變量 $pagenow 的返回值與 $hook_suffix 類似,只是它在前台後台都可以訪問,定義的更早,例如前三者在 admin_init 處沒有值,但 $pagenow 卻有。

它定義在 wp-includes/vars.php 中,該文件還定義了瀏覽器、服務器全局變量,例如 $is_winIE 、 $is_apache,wp_is_mobile() 函數也在這裏出現。

上述全局變量和函數能區分大多數情況,但依然有無力的時候,這時可以藉助 $_REQUEST 來判斷。上述變量的值也是從 $_REQUEST 獲取,但多一層值是否存在的檢查,所以能用它們解決的就不要用 $_REQUEST 或者 $_GET 。