资产置换

即使它不是最佳实践,有时你需要替换 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' );