current_screen
云策文档标注
概述
current_screen 是一个 WordPress 管理后台的 Hook,在屏幕识别元素设置完成后触发,用于访问当前屏幕的 WP_Screen 对象。
关键要点
- Hook 类型:admin hook,在屏幕设置后触发
- 参数:传递一个 WP_Screen 对象作为参数,包含屏幕的详细信息如 id、base、post_type、taxonomy 等
- 用途:常用于根据当前屏幕上下文执行特定操作,如自定义文章类型编辑或小工具页面管理
代码示例
/**
* Example of current_screen usage
* @param $current_screen
*/
function wporg_current_screen_example( $current_screen ) {
if ( 'someposttype' == $current_screen->post_type && 'post' === $current_screen->base ) {
// Do something in the edit screen of this post type
}
}
add_action( 'current_screen', 'wporg_current_screen_example' );/**
* Hides out the desired sidebar area from the widgets admin page.
*
* @param WP_Screen $current_screen Current WP_Screen object.
* @return void.
*/
function prefix_hide_sidebar( $current_screen ) {
global $wp_registered_sidebars;
$sidebar_id = 'sidebar-1';
if ( 'widgets' === $current_screen->id ) {
if ( array_key_exists( $sidebar_id, $wp_registered_sidebars ) ) {
unset( $wp_registered_sidebars[ $sidebar_id ] );
}
}
}
add_action( 'current_screen', 'prefix_hide_sidebar', 10, 1 );注意事项
- WP_Screen 对象包含私有和受保护属性,如 columns、_help_tabs 等,需注意访问权限
- Hook 在 WordPress 3.0.0 版本引入,确保兼容性
- 使用前应检查屏幕属性(如 id、base)以避免误操作
原文内容
Fires after the current screen has been set.
Parameters
Source
do_action( 'current_screen', $current_screen );
Changelog
| Version | Description |
|---|---|
| 3.0.0 | Introduced. |
Skip to note 3 content
Collins Mbaka
/** * Example of current_screen usage * @param $current_screen */ function wporg_current_screen_example( $current_screen ) { if ( 'someposttype' == $current_screen->post_type && 'post' === $current_screen->base ) { // Do something in the edit screen of this post type } } add_action( 'current_screen', 'wporg_current_screen_example' );Skip to note 4 content
Mahdi Yazdani
Hide the desired sidebar area from the Widgets admin page.
/** * Hides out the desired sidebar area from the widgets admin page. * * @param WP_Screen $current_screen Current WP_Screen object. * @return void. */ function prefix_hide_sidebar( $current_screen ) { global $wp_registered_sidebars; $sidebar_id = 'sidebar-1'; if ( 'widgets' === $current_screen->id ) { if ( array_key_exists( $sidebar_id, $wp_registered_sidebars ) ) { unset( $wp_registered_sidebars[ $sidebar_id ] ); } } } add_action( 'current_screen', 'prefix_hide_sidebar', 10, 1 );