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.
$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! 🙂
Thanx 1 Million for dev_check_current_screen!
My Dev-WP-Tool of the Day!
Thanks, Daniela. Glad you found it useful. 🙂
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!
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/
Cool!
Nice resource you linked to, too. Saves me the trouble of creating something similar! 🙂
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 )
Thanks, Michael.
The original article is quite old now, and I need to update it. 🙂