get_current_screen()
Gets the data object of the current admin panel page.
With this function, you can, for example, determine the current admin page and use this in a condition to, for example, display something on the desired admin page. Or, for example, separate the script's work for Pages (page) and Posts (post) in the admin panel.
current_screen — this is the earliest hook at which you can use this function. Before this hook, the data about the admin page has not yet been collected and the function will return null.
This function will return null in an AJAX request and will cause a Fatal Error on the front end (it is not defined on the front end).
$current_screen (data for this function) is set late. After the init event, when the HTML of the admin page begins to be generated. The definition of the variable $current_screen occurs in the file wp-admin/admin.php by the function set_current_screen(). The admin.php file is included throughout the admin panel.
No Hooks.
Returns
WP_Screen|null. WP_Screen data object or null when the global variable $current_screen is not set:
WP_Screen Object ( [id] => // String. Unique screen ID. [action] => // Action associated with the screen 'add' for *-new.php. Or empty result. [base] => post // Base type of the screen. For example, for pages of type 'post-new.php' or 'post.php' the base type = 'post'. [parent_base] => edit // Base type of the parent menu item. Obtained from $parent_file, query parameters are removed and // '.php' extension. E.g. 'edit.php?post_type=page' and 'edit.php?post_type=post' will have a parent base 'edit' [parent_file] => edit.php // Parent file of the screen from the menu, e.g.: 'edit.php?post_type=page', 'edit.php', 'options-general.php' [post_type] => post // Post type associated with the screen, if any. E.g.: 'edit.php?post_type=page' = 'page' [taxonomy] => // Taxonomy associated with the screen, if any. E.g.: 'edit-tags.php?taxonomy=category' = 'category' ... (different data) )
Examples
#1 Table of admin screens id in WordPress
| Screen | File | $screen->id |
|---|---|---|
| Comments | /wp-admin/edit-comments.php | edit-comments | |
| Themes | /wp-admin/themes.php | themes | |
| Plugins | /wp-admin/plugins.php | plugins | |
| Post | /wp-admin/post.php | post | |
| Post (new) | /wp-admin/post-new.php | post | |
| Posts | /wp-admin/edit.php | edit-post | |
| Pages | /wp-admin/edit.php | edit-page | |
| Post type | /wp-admin/edit.php | edit-{$post_type} | |
| Terms | /wp-admin/edit-tags.php | edit-{$taxonomy} | |
| Categories | /wp-admin/edit-tags.php | edit-category | |
| Tags | /wp-admin/edit-tags.php | edit-post_tag | |
| Media | /wp-admin/upload.php | upload | |
| Users | /wp-admin/users.php | users | |
| MS Sites | /wp-admin/network/sites.php | sites-network | |
| MS Themes | /wp-admin/network/themes.php | themes-network | |
| MS Users | /wp-admin/network/users.php | users-network | |
| MS Plugins | /wp-admin/network/plugins.php | plugins-network | |
| MC Site: Themes | /wp-admin/network/site-themes.php | site-themes-network | |
| MC Site: Users | /wp-admin/network/site-users.php | site-users-network |
#2 Run some code on the Widgets page
add_action( 'current_screen', 'wpkama_widgets_screen' );
function wpkama_widgets_screen(){
$screen = get_current_screen();
if( 'widgets' === $screen->id ){
// only on the admin page: Widgets
}
} #3 Let's add the script to the post editing page only
This example shows how to add javascript code to the footer of the admin panel, only on edit/create posts pages.
The text is added through the admin_footer filter, which checks the information of the current screen, if it is the edit posts page, the code is displayed:
<?php
// save the post in wordpress with ctrl + s
add_filter( 'admin_footer', 'post_save_accesskey' );
function post_save_accesskey(){
// exit if this is not the post creation/editing page
if( get_current_screen()->id !== 'post' )
return;
// or you can do it like this
// if( get_current_screen()->parent_base !== 'edit' )
// return;
?>
<script type="text/javascript">
jQuery(document).ready(function($){
// jQuery code
});
</script>
<?php
}
in this example get_current_screen() contains such an object:
WP_Screen Object ( [action] => add [base] => post [columns:WP_Screen:private] => 2 [id] => page [in_admin:protected] => site [is_network] => [is_user] => [parent_base] => edit [parent_file] => edit.php?post_type=page [post_type] => page [taxonomy] => [_help_tabs:WP_Screen:private] => Array ( [about-pages] => Array ( [title] => About pages [id] => about-pages [content] => <p>Pages are similar to posts in that they have a title, text, and metadata, but differ from them in that they do not belong to the chronological flow of the blog, but are persistent. Pages are not assigned headings and tags, but they can have a hierarchy. You can place some pages under others by selecting a parent page and getting a group of pages.</p><p>Creating a page is very similar to creating posts —The screen can be customized in the same way with drag-and-drop, the tab «Screen Settings» and minimize/expand blocks. This screen also has a handy full-screen mode, accessible in both visual and text editor by a special button. The page editor works the same way as the post editor, but in the «Page attributes» block there are several elements specific to pages:</p> [callback] => ) [inserting-media] => Array ( [title] => Insert media files [id] => inserting-media [content] => <p>You can upload and insert media files (images, audio files, documents, etc.) by clicking the «Add Media File» button. You can select images and files already uploaded to the library, or you can upload new files and add them to a post or page. To create a gallery, select images and click the «Create New Gallery» button.</p><p>You can also add media files from many popular sites, including Twitter, YouTube, Flickr and others, simply by copying the file address and pasting it into your post or page text on a separate line. Read the Codex article to <a href="http://codex.wordpress.org/Вставка_объектов">learn more about pasting objects</a>.</p> [callback] => ) [page-attributes] => Array ( [title] => Page Attributes [id] => page-attributes [content] => <p><strong>Parent</strong> — you can organize the page hierarchy. For example, create a page «About me» which will have pages «Biography» and «My dog» inside it. There are no limitations by nesting level.</p><p><strong>Template</strong> — in some themes for certain pages there may be special templates with additional features or a different design. You will see them in this list.</p><p><strong>Order</strong> — pages are usually sorted alphabetically, but you can set your own order by entering a number (1 — first, etc.) in this field.</p> [callback] => ) ) [_help_sidebar:WP_Screen:private] => <p><strong>More info:</strong></p><p><a href="http://codex.wordpress.org/Pages_Add_New_Screen" target="_blank">Documentation on adding new pages</a></p><p><a href="http://codex.wordpress.org/Pages_Screen#Editing_Individual_Pages" target="_blank">Documentation on editing pages</a></p><p><a href="http://ru.forums.wordpress.org/" target="_blank">Support Forums</a></p> [_options:WP_Screen:private] => Array ( [layout_columns] => Array ( [max] => 2 [default] => 2 ) ) [_show_screen_options:WP_Screen:private] => 1 [_screen_settings:WP_Screen:private] => <div class="editor-expand hidden"><label for="editor-expand-toggle"><input type="checkbox" id="editor-expand-toggle" checked='checked' />Stretch editor by window height; )
#4 Help tab
This example shows how to create a context help on the admin page you created. The page is created using add_options_page(), add_menu_page() and similar.
Let's say we created a page with the shortcut 'my_admin_page', which is located in the Options menu item.
add_action( 'admin_menu', 'my_admin_add_page' );
function my_admin_add_page(){
$page_hook = add_options_page( 'Settings page', 'Settings page', 'manage_options', 'my_opt_page_slug', 'my_admin_page' );
// add context when the page loads
add_action( "load-{$page_hook}", 'my_admin_add_help_tab' );
}
function my_admin_page(){
echo 'Page code';
}
function my_admin_add_help_tab(){
$screen = get_current_screen();
// content for the help tab
$screen->add_help_tab( array(
'id' => 'my_help_tab',
'title' => 'Page Help',
'content' => '<p>A supporting description explaining incomprehensible places on the page</p>',
) );
}
Notes
- Global. WP_Screen.
$current_screenWordPress current screen object.
Changelog
| Since 3.1.0 | Introduced. |
get_current_screen() get current screen code WP 6.9.1
function get_current_screen() {
global $current_screen;
if ( ! isset( $current_screen ) ) {
return null;
}
return $current_screen;
}