Get the current screen object
Source
function get_current_screen() {
global $current_screen;
if ( ! isset( $current_screen ) ) {
return null;
}
return $current_screen;
}
Changelog
| Version | Description |
|---|---|
| 3.1.0 | Introduced. |
Get the current screen object
function get_current_screen() {
global $current_screen;
if ( ! isset( $current_screen ) ) {
return null;
}
return $current_screen;
}
| Version | Description |
|---|---|
| 3.1.0 | Introduced. |
You must log in before being able to contribute a note or feedback.
This could be useful in unit tests
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.Another Example:
I was trying to find out the name of the admin dashboard.
You can find out the current screen name by dumping it:
Using this, I found the dashboard screen is just called ‘dashboard’ 🤯, so I could then redirect to a different page like this:
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.The fields returned are:
id(string) The unique ID of the screenaction(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:
Note that this function only works in admin!
This can be useful for conditionally executing code depending on which admin screen is currently being viewed.
Default Usage
This example shows how you would add contextual help to an admin page you’ve created with the
add_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.This function is useful to figure out which screen you’re on in the Dashboard,
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.