wp_dequeue_script()
云策文档标注
概述
wp_dequeue_script() 函数用于从 WordPress 队列中移除一个已入队的脚本。它通过调用 wp_scripts()->dequeue() 实现,确保脚本不会在前端加载。
关键要点
- 函数接受一个必需的字符串参数 $handle,指定要移除的脚本名称。
- 内部调用 _wp_scripts_maybe_doing_it_wrong() 进行错误检查,确保正确使用。
- 通常与 wp_print_scripts 动作钩子结合使用,以在脚本输出前移除。
代码示例
/**
* Dequeue the jQuery UI script.
*
* Hooked to the wp_print_scripts action, with a late priority (100),
* so that it is after the script was enqueued.
*/
function wpdocs_dequeue_script() {
wp_dequeue_script( 'jquery-ui-core' );
}
add_action( 'wp_print_scripts', 'wpdocs_dequeue_script', 100 );注意事项
- 确保在脚本被入队后调用此函数,例如使用 wp_print_scripts 钩子并设置较晚的优先级。
- 函数自 WordPress 3.1.0 版本引入,相关类为 WP_Dependencies::dequeue()。
原文内容
Removes a previously enqueued script.
Description
See also
Parameters
$handlestringrequired-
Name of the script to be removed.
Source
function wp_dequeue_script( $handle ) {
_wp_scripts_maybe_doing_it_wrong( __FUNCTION__, $handle );
wp_scripts()->dequeue( $handle );
}
Changelog
| Version | Description |
|---|---|
| 3.1.0 | Introduced. |
Skip to note 2 content
Codex
Dequeue a script
/** * Dequeue the jQuery UI script. * * Hooked to the wp_print_scripts action, with a late priority (100), * so that it is after the script was enqueued. */ function wpdocs_dequeue_script() { wp_dequeue_script( 'jquery-ui-core' ); } add_action( 'wp_print_scripts', 'wpdocs_dequeue_script', 100 );