-
If you want to set the screen to the front page do this:
set_current_screen( 'front' );This could be useful in unit tests
get_current_screen() – Function | Developer.WordPress.org
Get the current screen object
Return
WP_Screen|null Current screen object or null when screen not defined.Source
function get_current_screen() {
global $current_screen;
if ( ! isset( $current_screen ) ) {
return null;
}
return $current_screen;
}
| Used by | Description |
|---|---|
WP_Plugin_Dependencies::display_admin_notice_for_unmet_dependencies()wp-includes/class-wp-plugin-dependencies.php | Displays an admin notice if dependencies are not installed. |
wp_global_styles_render_svg_filters()wp-includes/deprecated.php | Renders the SVG filters supplied by theme.json. |
WP_Site_Health::admin_body_class()wp-admin/includes/class-wp-site-health.php | Adds a class to the body HTML tag. |
WP_Site_Health::enqueue_scripts()wp-admin/includes/class-wp-site-health.php | Enqueues the site health scripts. |
WP_Privacy_Policy_Content::notice()wp-admin/includes/class-wp-privacy-policy-content.php | Adds a notice with a link to the guide when editing the privacy policy page. |
WP_Privacy_Policy_Content::policy_text_changed_notice()wp-admin/includes/class-wp-privacy-policy-content.php | Outputs a warning when some privacy info has changed. |
_wp_privacy_settings_filter_draft_page_titles()wp-admin/includes/misc.php | Appends ‘(Draft)’ to draft page titles in the privacy page dropdown so that unpublished content is obvious. |
WP_Widget_Custom_HTML::add_help_text()wp-includes/widgets/class-wp-widget-custom-html.php | Add help text to widgets admin screen. |
wp_ajax_search_install_plugins()wp-admin/includes/ajax-actions.php | Handles searching plugins to install via AJAX. |
wp_ajax_search_plugins()wp-admin/includes/ajax-actions.php | Handles searching plugins via AJAX. |
WP_Screen::render_view_mode()wp-admin/includes/class-wp-screen.php | Renders the list table view mode preferences. |
WP_Screen::get()wp-admin/includes/class-wp-screen.php | Fetches a screen object. |
add_screen_option()wp-admin/includes/screen.php | Register and configure an admin screen option |
screen_layout()wp-admin/includes/deprecated.php | Returns the screen layout options. |
screen_options()wp-admin/includes/deprecated.php | Returns the screen’s per-page options. |
screen_meta()wp-admin/includes/deprecated.php | Renders the screen’s help. |
wp_plugin_update_row()wp-admin/includes/update.php | Displays update information for a plugin. |
wp_add_dashboard_widget()wp-admin/includes/dashboard.php | Adds a new dashboard widget. |
wp_dashboard()wp-admin/includes/dashboard.php | Displays the dashboard. |
wp_dashboard_setup()wp-admin/includes/dashboard.php | Registers dashboard widgets. |
_get_list_table()wp-admin/includes/list-table.php | Fetches an instance of a WP_List_Table class. |
iframe_header()wp-admin/includes/template.php | Generic Iframe header for use with Thickbox. |
add_meta_box()wp-admin/includes/template.php | Adds a meta box to one or more screens. |
do_meta_boxes()wp-admin/includes/template.php | Meta-Box template function. |
remove_meta_box()wp-admin/includes/template.php | Removes a meta box from one or more screens. |
do_accordion_sections()wp-admin/includes/template.php | Meta Box Accordion Template Function. |
post_comment_meta_box()wp-admin/includes/meta-boxes.php | Displays comments for post. |
page_attributes_meta_box()wp-admin/includes/meta-boxes.php | Displays page attributes form fields. |
Custom_Image_Header::help()wp-admin/includes/class-custom-image-header.php | Adds contextual help. |
Custom_Background::admin_load()wp-admin/includes/class-custom-background.php | Sets up the enqueue for the CSS & JavaScript files. |
wp_auth_check_load()wp-includes/functions.php | Loads the auth check for monitoring whether the user is still logged in. |
wp_admin_bar_edit_menu()wp-includes/admin-bar.php | Provides an edit link for posts and terms. |
Changelog
| Version | Description |
|---|---|
| 3.1.0 | Introduced. |
User Contributed Notes
-
Skip to note 11 content -
Skip to note 12 content Be aware the this function doesn’t always exist, something that @ravanh had sort of eluded to. It isn’t loaded and available until after
admin_inithas fired. It is advisable to check whether the function exists when using it within any hooks in the even those hooks fire before that function is actually loaded and available to use./** * Check whether the get_current_screen function exists * because it is loaded only after 'admin_init' hook. */ if ( function_exists( 'get_current_screen' ) ) { $current_screen = get_current_screen(); // Do your thing. } -
Skip to note 13 content Another Example:
<?php 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 } } ?> -
Skip to note 14 content I was trying to find out the name of the admin dashboard.
You can find out the current screen name by dumping it:$my_current_screen = get_current_screen(); var_dump( $my_current_screen->base );Using this, I found the dashboard screen is just called ‘dashboard’ 🤯, so I could then redirect to a different page like this:
/** * Redirect specific admin page */ add_action( 'current_screen', 'wpdocs_custom_redirect_admin_dashboard' ); function wpdocs_custom_redirect_admin_dashboard() { if ( is_admin() ) { $my_current_screen = get_current_screen(); if ( isset( $my_current_screen->base ) && 'dashboard' === $my_current_screen->base ) { wp_redirect( admin_url().'?page=custom_dashboard' ); exit(); } } } -
Skip to note 15 content The fields returned are:
id(string) The unique ID of the screen
action(string) Any action associated with the screen. ‘add’ for *-new.php screens. Empty otherwise.
base(string) The base type of the screen. For example, for a page ‘post-new.php’ the base is ‘post’.
parent_base(string) The base menu parent. This is derived from $parent_file by removing the query string and any .php extension. For example, parent_file values of ‘edit.php?post_type=page’ and ‘edit.php?post_type=post’ have a parent_base of ‘edit’
parent_file(string) The parent_file for the screen per the admin menu system. Some parent_file values are ‘edit.php?post_type=page’, ‘edit.php’, and ‘options-general.php’
post_type(string) The post type associated with the screen, if any. For example, the ‘edit.php?post_type=page’ screen has a post type of ‘page’
taxonomy(string) The taxonomy associated with the screen, if any. For example, the ‘edit-tags.php?taxonomy=category’ screen has a taxonomy of ‘category’Returned object:
WP_Screen Object ( [action] => [base] => post [id] => post [is_network] => [is_user] => [parent_base] => edit [parent_file] => edit.php [post_type] => post [taxonomy] => ... (private fields) ) -
Skip to note 16 content Be aware that at the global
$current_screengets set relatively late, right after theadmin_inithas run. This is done with functionset_current_screen()in template files like admin-header.php and admin.php or when processing admin ajax requests.Calling
set_current_screen()yourself earlier will not help. Althoughget_current_screen()does not return null anymore, the current screen object will not be populated with the correct properties, likeget_current_screen()->$id, to work with.Use
get_current_screen()at thecurrent_screenhook or later. -
Skip to note 17 content Note that this function only works in admin!
-
Skip to note 18 content This can be useful for conditionally executing code depending on which admin screen is currently being viewed.
// Hook into the admin_head action to run your code in the admin area add_action( 'admin_head', 'wpdocs_admin_code' ); function wpdocs_admin_code() { // Get the current screen object $current_screen = get_current_screen(); // Check if the current screen is the dashboard if ( 'dashboard' === $current_screen->id ) { // Execute your code for the dashboard screen echo 'alert( "You are on the dashboard!" );'; } // Check if the current screen is a post editing screen if ( 'post' === $current_screen->base ) { // Execute your code for the post editing screen echo 'alert( "You are editing a post!" );'; } // Check for other screen IDs or bases as needed // For example, to check if it's a page editing screen: if ( 'page' === $current_screen->id ) { // Execute your code for the page editing screen echo 'alert( "You are editing a page!" );'; } // More conditions can be added as needed } -
Skip to note 19 content Default Usage
This example shows how you would add contextual help to an admin page you’ve created with theadd_options_page()function. Here, we assume that your admin page has a slug ofmy_admin_pageand exists under the Options tab.The
get_current_screen()function is used in this example.<?php 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>', ) ); } ?> -
Skip to note 20 content This function is useful to figure out which screen you’re on in the Dashboard,
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; } .... }Another useful attribute of the WP_Screen object returned by this function is the
$screen->baseattribute which is set to'post'for any custom post edit screen, and is set to'edit'for the custom post admin table page, which is very handy when loading custom css/scripts for additional function on those pages.