current_screen
An admin event hook that fires after the elements required to identify the current screen have been set up. It passes a WP_Screen object as its parameter.
This hook is useful whenever an action should run only on a particular admin page. For example, a custom script may need to be enqueued only on a post editing page or a page created by a plugin.
Usage
add_action( 'current_screen', 'action_function_name_9874' );
function action_function_name_9874( $current_screen ) {
// Action...
}
Parameters
- $current_screen(object)
- A WP_Screen instance containing data about the current screen.
The $current_screen parameter
The event passes a WP_Screen instance in $current_screen. This example shows the data passed on the post editing page (post.php):
WP_Screen Object ( [action] => [base] => post [columns:WP_Screen:private] => 0 [id] => post [in_admin:protected] => site [is_network] => [is_user] => [parent_base] => [parent_file] => [post_type] => post [taxonomy] => [_help_tabs:WP_Screen:private] => Array ( ) [_help_sidebar:WP_Screen:private] => [_options:WP_Screen:private] => Array ( ) [_show_screen_options:WP_Screen:private] => [_screen_settings:WP_Screen:private] => )
For comparison, this is the data passed on the category list page:
WP_Screen Object ( [action] => [base] => edit-tags [columns:WP_Screen:private] => 0 [id] => edit-category [in_admin:protected] => site [is_network] => [is_user] => [parent_base] => [parent_file] => [post_type] => post [taxonomy] => category [_help_tabs:WP_Screen:private] => Array ( ) [_help_sidebar:WP_Screen:private] => [_options:WP_Screen:private] => Array ( ) [_show_screen_options:WP_Screen:private] => [_screen_settings:WP_Screen:private] => )
Examples
#1 Usage example
Suppose there is a custom post type named book, and an action must run on its admin editing page:
add_action( 'current_screen', 'current_screen_hook' );
function current_screen_hook( $current_screen ){
if ( 'book' == $current_screen->post_type && 'post' == $current_screen->base ) {
// Do something on the book post type editing page.
}
}
#2 Add filters only on taxonomy list pages
This example attaches a filter on admin taxonomy list pages, such as category and tag lists. It outputs custom text in the admin footer only on those pages:
add_action( 'current_screen', 'kkk_my_func' );
function kkk_my_func( $current_screen ){
if( 'edit-tags' != $current_screen->base )
return;
add_action('admin_footer_text', 'my_admin_footer_function_name');
}
function my_admin_foofter_function_name(){
echo "This is a taxonomy list page.";
}Changelog
| Since 3.0.0 | Introduced. |
Where the hook is called
do_action( 'current_screen', $current_screen );