print_head_scripts()
云策文档标注
概述
print_head_scripts() 是一个 WordPress 函数,用于在管理页面(admin pages)的 HTML head 部分打印脚本队列。它处理脚本的延迟加载,确保脚本在适当位置输出。
关键要点
- 函数作用:在管理页面的 HTML head 中打印脚本队列,支持脚本的延迟和依赖处理。
- 返回值:返回一个字符串数组,包含已打印脚本的句柄(handles)。
- 相关函数:与 print_footer_scripts() 和 wp_print_scripts() 配合使用,分别处理页脚和前端脚本。
- Hook 支持:提供 apply_filters('print_head_scripts', bool $print) 过滤器,允许控制是否打印 head 脚本;do_action('wp_print_scripts') 在脚本打印前触发。
- 内部流程:初始化 $wp_scripts,设置脚本连接选项,处理 head 组项目,并通过 _print_scripts() 输出。
代码示例
function print_head_scripts() {
global $concatenate_scripts;
if ( ! did_action( 'wp_print_scripts' ) ) {
/** This action is documented in wp-includes/functions.wp-scripts.php */
do_action( 'wp_print_scripts' );
}
$wp_scripts = wp_scripts();
script_concat_settings();
$wp_scripts->do_concat = $concatenate_scripts;
$wp_scripts->do_head_items();
/**
* Filters whether to print the head scripts.
*
* @since 2.8.0
*
* @param bool $print Whether to print the head scripts. Default true.
*/
if ( apply_filters( 'print_head_scripts', true ) ) {
_print_scripts();
}
$wp_scripts->reset();
return $wp_scripts->done;
}注意事项
- 此函数主要用于管理页面,前端页面应使用 wp_print_head_scripts()。
- 确保在调用前已正确初始化脚本队列,以避免脚本输出错误。
- 通过过滤器 print_head_scripts 可以灵活控制脚本打印行为,例如在特定条件下禁用。
原文内容
Prints the script queue in the HTML head on admin pages.
Description
Postpones the scripts that were queued for the footer.
print_footer_scripts() is called in the footer to print these scripts.
See also
Source
function print_head_scripts() {
global $concatenate_scripts;
if ( ! did_action( 'wp_print_scripts' ) ) {
/** This action is documented in wp-includes/functions.wp-scripts.php */
do_action( 'wp_print_scripts' );
}
$wp_scripts = wp_scripts();
script_concat_settings();
$wp_scripts->do_concat = $concatenate_scripts;
$wp_scripts->do_head_items();
/**
* Filters whether to print the head scripts.
*
* @since 2.8.0
*
* @param bool $print Whether to print the head scripts. Default true.
*/
if ( apply_filters( 'print_head_scripts', true ) ) {
_print_scripts();
}
$wp_scripts->reset();
return $wp_scripts->done;
}
Hooks
- apply_filters( ‘print_head_scripts’, bool $print )
-
Filters whether to print the head scripts.
- do_action( ‘wp_print_scripts’ )
-
Fires before scripts in the $handles queue are printed.
Changelog
| Version | Description |
|---|---|
| 2.8.0 | Introduced. |