get_current_screen()WP 3.1.0

Get the current screen object

1 time — 0.000016 sec (very fast) | 50000 times — 0.01 sec (speed of light)

No Hooks.

Return

WP_Screen|null. Current screen object or null when screen not defined.

Usage

get_current_screen();

Examples

0

#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
0

#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
	}
}
0

#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;
)
0

#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_screen WordPress current screen object.

Changelog

Since 3.1.0 Introduced.

Code of get_current_screen() WP 6.0.1

function get_current_screen() {
	global $current_screen;

	if ( ! isset( $current_screen ) ) {
		return null;
	}

	return $current_screen;
}