wp_print_head_scripts()
云策文档标注
概述
wp_print_head_scripts() 是一个 WordPress 函数,用于在前端 HTML head 中打印脚本队列。它推迟了原本排队在页脚的脚本,这些脚本随后由 wp_print_footer_scripts() 在页脚打印。
关键要点
- 函数功能:打印前端 HTML head 中的脚本队列,并推迟页脚脚本。
- 返回值:返回已打印脚本的句柄数组。
- 触发动作:在打印脚本前触发 wp_print_scripts 动作钩子。
- 依赖条件:需要 $wp_scripts 是 WP_Scripts 实例,否则返回空数组。
代码示例
function wp_print_head_scripts() {
global $wp_cripts;
if ( ! did_action( 'wp_print_scripts' ) ) {
/** This action is documented in wp-includes/functions.wp-scripts.php */
do_action( 'wp_print_scripts' );
}
if ( ! ( $wp_cripts instanceof WP_Scripts ) ) {
return array(); // No need to run if nothing is queued.
}
return print_head_scripts();
}注意事项
- 确保在调用前已触发 wp_print_scripts 动作,否则函数会触发它。
- 如果 $wp_scripts 未实例化或为空,函数将返回空数组,避免不必要的执行。
- 相关函数包括 wp_print_footer_scripts()、print_head_scripts() 和 did_action()。
原文内容
Prints the script queue in the HTML head on the front end.
Description
Postpones the scripts that were queued for the footer.
wp_print_footer_scripts() is called in the footer to print these scripts.
Source
function wp_print_head_scripts() {
global $wp_scripts;
if ( ! did_action( 'wp_print_scripts' ) ) {
/** This action is documented in wp-includes/functions.wp-scripts.php */
do_action( 'wp_print_scripts' );
}
if ( ! ( $wp_scripts instanceof WP_Scripts ) ) {
return array(); // No need to run if nothing is queued.
}
return print_head_scripts();
}
Hooks
- do_action( ‘wp_print_scripts’ )
-
Fires before scripts in the $handles queue are printed.
Changelog
| Version | Description |
|---|---|
| 2.8.0 | Introduced. |