wp_enqueue_admin_bar_header_styles()
云策文档标注
概述
wp_enqueue_admin_bar_header_styles() 函数用于在打印页面时隐藏管理工具栏,通过添加内联 CSS 样式实现。该函数还处理向后兼容性,确保插件禁用相关功能时不会出错。
关键要点
- 函数 wp_enqueue_admin_bar_header_styles() 在 WordPress 6.4.0 版本中引入。
- 它使用 wp_add_inline_style() 添加 CSS 媒体查询,在打印时隐藏 #wpadminbar。
- 函数检查是否有插件通过移除 wp_admin_bar_header 动作来禁用功能,并相应调整以避免冲突。
- 相关函数包括 has_action()、remove_action() 和 is_admin(),用于钩子管理和条件判断。
代码示例
function wp_enqueue_admin_bar_header_styles() {
// Back-compat for plugins that disable functionality by unhooking this action.
$action = is_admin() ? 'admin_head' : 'wp_head';
if ( ! has_action( $action, 'wp_admin_bar_header' ) ) {
return;
}
remove_action( $action, 'wp_admin_bar_header' );
wp_add_inline_style( 'admin-bar', '@media print { #wpadminbar { display:none; } }' );
}
原文内容
Enqueues inline style to hide the admin bar when printing.
Source
function wp_enqueue_admin_bar_header_styles() {
// Back-compat for plugins that disable functionality by unhooking this action.
$action = is_admin() ? 'admin_head' : 'wp_head';
if ( ! has_action( $action, 'wp_admin_bar_header' ) ) {
return;
}
remove_action( $action, 'wp_admin_bar_header' );
wp_add_inline_style( 'admin-bar', '@media print { #wpadminbar { display:none; } }' );
}
Changelog
| Version | Description |
|---|---|
| 6.4.0 | Introduced. |