What is the $current_screen global variable?

What does it mean by a screen? I’ve had a look in the codex and don’t understand (I’m a newbie). Thanks.

Related posts

2 comments

  1. $current_screen is a global variable in admin screens. It holds the result of WP_Screen::get( $hook_name )->set_current_screen();.

    WP_Screen is a class, get() is a static method that returns a real object – an instance of WP_Screen with some predefined properties:

    $screen->base       = $base;
    $screen->action     = $action;
    $screen->post_type  = (string) $post_type;
    $screen->taxonomy   = (string) $taxonomy;
    $screen->is_user    = ( 'user' == $in_admin );
    $screen->is_network = ( 'network' == $in_admin );
    $screen->in_admin   = $in_admin;
    

    $hook_name is a page specific variable for different admin pages.

    set_current_screen() pushes the object properties into the global variable. I consider this as a rather poor step. It is done for backwards compatibility, I guess.

    You can use $current_screen on plugin pages, in metaboxes and other places to get some context information.

    Example:

    if ( $GLOBALS['current_screen']->is_network and is_super_admin() )
    {
        // do something specific for network admins
    }
    
  2. I know now. It retrieves the current settings page you are on. So for example, if you’re on the the theme options page, it can check to make sure you are.

    The $current_screen global variable contains the following elements:

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

    So you can check if you’re on a post type, for example:

    function change_default_title ( $title ) {
    $screen = get_current_screen() ; 
    if ( ' POST_TYPE' == $screen->post_type ) {
    $title = 'Enter Invoice title ';
    }
    return $title; 
    }
    add_filter ( 'enter_title_here', 'change_default_title' );
    

Comments are closed.