get_current_screen()
Get the current screen object
Return #Return
(WP_Screen|null) Current screen object or null when screen not defined.
Source #Source
File: wp-admin/includes/screen.php
function get_current_screen() { global $current_screen; if ( ! isset( $current_screen ) ) { return null; } return $current_screen; }
Expand full source code Collapse full source code View on Trac
Related #Related
Used By #Used By
Used By | Description |
---|---|
wp-admin/includes/class-wp-site-health.php: WP_Site_Health::admin_body_class() |
Add a class to the body HTML tag. |
wp-admin/includes/class-wp-site-health.php: WP_Site_Health::enqueue_scripts() |
Enqueues the site health scripts. |
wp-admin/includes/class-wp-privacy-policy-content.php: WP_Privacy_Policy_Content::notice() |
Add a notice with a link to the guide when editing the privacy policy page. |
wp-admin/includes/class-wp-privacy-policy-content.php: WP_Privacy_Policy_Content::policy_text_changed_notice() |
Output a warning when some privacy info has changed. |
wp-admin/includes/misc.php: _wp_privacy_settings_filter_draft_page_titles() |
Appends ‘(Draft)’ to draft page titles in the privacy page dropdown so that unpublished content is obvious. |
wp-includes/widgets/class-wp-widget-custom-html.php: WP_Widget_Custom_HTML::add_help_text() |
Add help text to widgets admin screen. |
wp-admin/includes/ajax-actions.php: wp_ajax_search_install_plugins() |
Ajax handler for searching plugins to install. |
wp-admin/includes/ajax-actions.php: wp_ajax_search_plugins() |
Ajax handler for searching plugins. |
wp-admin/includes/class-wp-screen.php: WP_Screen::render_view_mode() |
Render the list table view mode preferences. |
wp-admin/includes/class-wp-screen.php: WP_Screen::get() |
Fetches a screen object. |
wp-admin/includes/screen.php: add_screen_option() |
Register and configure an admin screen option |
wp-admin/includes/deprecated.php: screen_layout() |
Returns the screen layout options. |
wp-admin/includes/deprecated.php: screen_options() |
Returns the screen’s per-page options. |
wp-admin/includes/deprecated.php: screen_meta() |
Renders the screen’s help. |
wp-admin/includes/update.php: wp_plugin_update_row() |
Displays update information for a plugin. |
wp-admin/includes/dashboard.php: wp_add_dashboard_widget() |
Adds a new dashboard widget. |
wp-admin/includes/dashboard.php: wp_dashboard() |
Displays the dashboard. |
wp-admin/includes/dashboard.php: wp_dashboard_setup() |
Registers dashboard widgets. |
wp-admin/includes/list-table.php: _get_list_table() |
Fetches an instance of a WP_List_Table class. |
wp-admin/includes/template.php: iframe_header() |
Generic Iframe header for use with Thickbox |
wp-admin/includes/template.php: add_meta_box() |
Adds a meta box to one or more screens. |
wp-admin/includes/template.php: do_meta_boxes() |
Meta-Box template function. |
wp-admin/includes/template.php: remove_meta_box() |
Removes a meta box from one or more screens. |
wp-admin/includes/template.php: do_accordion_sections() |
Meta Box Accordion Template Function. |
wp-admin/includes/meta-boxes.php: post_comment_meta_box() |
Display comments for post. |
wp-admin/includes/meta-boxes.php: page_attributes_meta_box() |
Display page attributes form fields. |
wp-admin/includes/class-custom-image-header.php: Custom_Image_Header::help() |
Adds contextual help. |
wp-admin/includes/class-custom-background.php: Custom_Background::admin_load() |
Set up the enqueue for the CSS & JavaScript files. |
wp-includes/functions.php: wp_auth_check_load() |
Load the auth check for monitoring whether the user is still logged in. |
wp-includes/admin-bar.php: wp_admin_bar_edit_menu() |
Provide an edit link for posts and terms. |
Changelog #Changelog
Version | Description |
---|---|
3.1.0 | Introduced. |
User Contributed Notes #User Contributed Notes
You must log in before being able to contribute a note or feedback.
Expand full source codeCollapse full source code
Feedback
If you want to set the screen to the front page do this: set_current_screen( ‘front’ ); This could be useful in unit tests — By carlosenam —
Another Example:
Expand full source codeCollapse full source code
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:
Expand full source codeCollapse full source code
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:
Expand full source codeCollapse full source code
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.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.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.Expand full source codeCollapse full source code