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;
}
Related
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. |
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_init
has 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_screen
gets set relatively late, right after theadmin_init
has 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_screen
hook 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_page
and 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->base
attribute 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.