資產置換

即使它不是最佳實踐,有時你需要替換 CSS 或 JS 檔案或庫等資產。

請注意,WordPress 模板覆蓋系統不能與 .php 檔案一起使用,所以當我們談論資產時,我們指的是註冊資產

一個例子可能是用你想要的版本替換 jQuery 庫。在我們的子主題 functions.php 檔案中,我們需要新增一個刪除當前 jQuery 版本的函式,並從 CDN 新增我們自己的版本(記住這只是一個例子)。

/**
 * Dequeue the jQuery script and add our own version.
 *
 * Hooked to the wp_print_scripts action, with a late priority (100),
 * so that it is after the script was enqueued.
 */
function my_own_theme_scripts() {
    // remove the current version
    wp_dequeue_script( 'jquery' );
    // register my desired version
    wp_register_script( 'jquery', 'https://code.jquery.com/jquery-3.1.0.min.js', false, '3.1.0' );
    // load my version, here or somewhere else
    wp_enqueue_script( 'jquery' );
}
add_action( 'wp_print_scripts', 'my_own_theme_scripts' );