WordPress $current_screen variable for admin screens

If you are a plugin or theme author and your plugin/theme does something with a Dashboard admin screen, good practice requires that you ensure that whatever your code is doing only affects the relevant screens. A typical example is loading javascript for a settings page or theme options page – you should only load it on your plugin or theme settings page.

In order to limit a plugin’s or theme’s action to a specific admin page, it’s necessary to determine which screen is loaded so that an appropriate if…else check can be performed. A common way of doing this is by checking against actual WordPress file names, $_SERVER or $_GET variables, and I use a variety of these methods in my own plugins.

Although such methods work, I’ve discovered that WordPress has a neat alternative tool at our disposal, the $current_screen global variable, which can be used in certain circumstances.

Note: the $current_screen variable is set by the set_current_screen() function which is run AFTER the admin_init and admin_head actions are fired. Therefore, it is no use as a tool for controlling the loading of javascript, or anything else that is hooked to admin_init or admin_head. It still has it’s uses though…

$current_screen object

The $current_screen global variable contains the following elements:

   'id' =>
   'base' =>
   'action' => 
   'parent_file' =>
   'parent_base' =>

Example: Dashboard > Media > Library page

stdClass::__set_state(array(
	'id' => 'upload',
	'base' => 'upload',
	'action' => '',
	'parent_file' => 'upload.php',
	'parent_base' => 'upload',
))

Example: Dashboard > Posts > Add New

stdClass::__set_state(array(
	'id' => 'post',
	'base' => 'post',
	'action' => 'add',
	'post_type' => 'post',
	'parent_file' => 'edit.php',
	'parent_base' => 'edit',
))

Development tip

Here’s a little function which you can use to see what is the current content of $current_screen, which can be useful during development:

add_action( 'admin_notices', 'dev_check_current_screen' );
function dev_check_current_screen() {
	if( !is_admin() ) return;
	
	global $current_screen;
	
	print_r($current_screen);
}

Note that admin_notices is run after $current_screen is set. As mentioned earlier, don’t bother trying to hook to admin_init or admin_head as these are fired before $current_screen is set.

When I have some time, I may post a list of all $current_screen id’s for WP 3.1. But don’t hold your breath – it’s pretty low on my current list of priorities! :-)

Comments

  1. Daniela says:

    Thanx 1 Million for dev_check_current_screen!

    My Dev-WP-Tool of the Day!

  2. Sorry to contact you via this method however my e-mail is not available at the moment. I have placed your DCG in two sites. One site works well (usaudio.com) the other (digitraise.net (exact same set up: WP-Genesis-Streamline)) is not working at all as I wish or know it should. If you would be so kind; I would like to ask that you provide me a Rate Quote by phone for your assistance. My #: (352) 281-3196 Jonathan Smith

    • Hi Jonathan,

      Please use my Support Forum for support questions. i’m happy to help out there…

      Thanks!

  3. This is exactly what I needed to know. Thanks!

    This post over here has a reference table for the different WordPress screen IDs which might be useful as well: http://cleverwp.com/current_screen-wordpress-global-variable/

  4. Michael says:

    Thanks for this. One addendum: looks like $current_screen is now set up directly after admin_init, so it’ll work using the admin_head hook.

    ( http://codex.wordpress.org/Plugin_API/Action_Reference#Actions_Run_During_an_Admin_Page_Request )

Leave a Comment

*


3 − three =

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>