获取当前屏幕对象
返回#返回
(WP_Screen | null)当前屏幕对象,或者屏幕未定义时为null。
add_action( 'current_screen', 'wpdocs_this_screen' );
/**
* Run code on the admin widgets page
*/
function wpdocs_this_screen() {
$currentScreen = get_current_screen();
if( $currentScreen->id === "widgets" ) {
// Run some code, only on the admin widgets page
}
}
//第二个
PAGE $SCREEN_ID FILE
----------------- ------------------- -----------------------
Media Library upload upload.php
Comments edit-comments edit-comments.php
Tags edit-post_tag edit-tags.php
Plugins plugins plugins.php
Links link-manager link-manager.php
Users users users.php
Posts edit-post edit.php
Pages edit-page edit.php
Edit Site: Themes site-themes-network network/site-themes.php
Themes themes-network network/themes
Users users-network network/users
Edit Site: Users site-users-network network/site-users
Sites
//第三个示例
add_action('trashed_post', 'trash_wpcrm_contact');
function trash_wpcrm_contact($post_id){
$screen = get_current_screen();
if('wpcrm-contact' != $screen->post_type){ //this is not our custom post, so let's exit
return;
}
....
}
//第三个介绍
此函数返回的WP_Screen对象的另一个有用属性$screen->base是'post'为任何自定义后期编辑屏幕设置的属性,并设置'edit'为自定义后期管理表页面,在为其他功能加载自定义CSS /脚本时非常方便在这些页面上。
默认用法
此示例显示如何将上下文帮助添加到使用该add_options_page()函数创建的管理页面。在这里,我们假设你的管理页面有一个slug,my_admin_page并存在于Options选项卡下。
该get_current_screen()示例中使用了该函数。
add_action('admin_menu', 'wpdocs_admin_add_page');
/**
* Add an admin page
*/
function wpdocs_admin_add_page() {
global $wpdocs_admin_page;
$wpdocs_admin_page = add_options_page(__('Wpdocs Admin Page', 'wpdocs_textdomain'),
__('Wpdocs Admin Page', 'wpdocs_textdomain'),
'manage_options', 'wpdocs_textdomain', 'wpdocs_admin_page');
// Adds my_help_tab when my_admin_page loads
add_action('load-'.$wpdocs_admin_page, 'wpdocs_admin_add_help_tab');
}
/**
* Add a contextual help tab to the Wpdocs Admin Page
*/
function wpdocs_admin_add_help_tab () {
global $wpdocs_admin_page;
$screen = get_current_screen();
/*
* Check if current screen is Wpdocs Admin Page
* Don't add help tab if it's not
*/
if ( $screen->id != $wpdocs_admin_page )
return;
// Add my_help_tab if current screen is My Admin Page
$screen->add_help_tab( array(
'id' => 'wpdocs_help_tab',
'title' => __('Wpdocs Help Tab'),
'content' => '<p>'
. __( 'Descriptive content that will show in Wpdocs Help Tab body goes here.', 'wpdocs_textdomain' )
. '</p>',
) );
}

