WordPress.org

Codex

Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Function Reference/get current screen

Description

This function returns an object that includes the screen’s ID, base, post type, and taxonomy, among other data points

Usage

 <?php $screen get_current_screen(); ?> 

Parameters

None

Usage Restrictions

This function is defined on most admin pages, but not all. Thus there are cases where is_admin() will return true, but attempting to call get_current_screen() will result in a fatal error because it is not defined. One known example is wp-admin/customize.php.

The function returns null if called from the admin_init hook. It should be OK to use in a later hook such as current_screen.

Return Value

Returns a WP_Screen object, or null if not applicable.

The fields returned are:

id 
(string) The unique ID of the screen
action 
(string) Any action associated with the screen. 'add' for *-new.php screens. Empty otherwise.
base 
(string) The base type of the screen. For example, for a page 'post-new.php' the base is 'post'.
parent_base 
(string) The base menu parent. This is derived from $parent_file by removing the query string and any .php extension. For example, parent_file values of 'edit.php?post_type=page' and 'edit.php?post_type=post' have a parent_base of 'edit'
parent_file 
(string) The parent_file for the screen per the admin menu system. Some parent_file values are 'edit.php?post_type=page', 'edit.php', and 'options-general.php'
post_type 
(string) The post type associated with the screen, if any. For example, the 'edit.php?post_type=page' screen has a post type of 'page'
taxonomy 
(string) The taxonomy associated with the screen, if any. For example, the 'edit-tags.php?taxonomy=category' screen has a taxonomy of 'category'

Example return value on a post edit page (as of WP version 3.3.1):

WP_Screen Object
(
    [action] => 
    [base] => post
    [id] => post
    [is_network] => 
    [is_user] => 
    [parent_base] => edit
    [parent_file] => edit.php
    [post_type] => post
    [taxonomy] => 
    ...
    (private fields)
)

Example

Default Usage

This example shows how you would add contextual help to an admin page you've created with the add_options_page() function. Here, we assume that your admin page has a slug of 'my_admin_page' and exists under the Options tab.

The get_current_screen() function is used in this example.

<?php 
add_action('admin_menu', 'my_admin_add_page');
function my_admin_add_page() {
    global $my_admin_page;
    $my_admin_page = add_options_page(__('My Admin Page', 'map'), __('My Admin Page', 'map'), 'manage_options', 'map', 'my_admin_page');

    // Adds my_help_tab when my_admin_page loads
    add_action('load-'.$my_admin_page, 'my_admin_add_help_tab');
}

function my_admin_add_help_tab () {
    global $my_admin_page;
    $screen = get_current_screen();

    /*
     * Check if current screen is My Admin Page
     * Don't add help tab if it's not
     */
    if ( $screen->id != $my_admin_page )
        return;

    // Add my_help_tab if current screen is My Admin Page
    $screen->add_help_tab( array(
        'id'	=> 'my_help_tab',
        'title'	=> __('My Help Tab'),
        'content'	=> '<p>' . __( 'Descriptive content that will show in My Help Tab-body goes here.' ) . '</p>',
    ) );
}
?>

Another Example:

<?php

add_action( 'current_screen', 'this_screen' );

function this_screen() {

    $current_screen = get_current_screen();

    if( $current_screen->id === "widgets" ) {

        // Run some code, only on the admin widgets page

    }
    
}

?>

Change Log

Source File

get_current_screen() since Version 3.1 is located in wp-admin/includes/template.php. Since Version 3.3 is moved to wp-admin/includes/screen.php.

Related

Topics

Functions

Resources

This page is marked as incomplete. You can help Codex by expanding it.