get_current_screen()

Get the current screen object


Description Description


Return Return

(WP_Screen|null) Current screen object or null when screen not defined.


Top ↑

Source Source

File: wp-admin/includes/screen.php

function get_current_screen() {
	global $current_screen;

	if ( ! isset( $current_screen ) )
		return null;

	return $current_screen;
}

Top ↑

Changelog Changelog

Changelog
Version Description
3.1.0 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note content
    Contributed by Codex

    Another Example:

    <?php
    
    add_action( 'current_screen', 'wpdocs_this_screen' );
    
    /**
     * Run code on the admin widgets page
     */
    function wpdocs_this_screen() {
        $currentScreen = get_current_screen();
        if( $currentScreen->id === "widgets" ) {
            // Run some code, only on the admin widgets page
        }
    }
    
    ?>
    
    
  2. Skip to note content
    Contributed by Aurovrata Venet

    This function is useful to figure out which screen you’re on in the Dashboard,

    add_action('trashed_post', 'trash_wpcrm_contact');
    function trash_wpcrm_contact($post_id){
        $screen = get_current_screen();
        if('wpcrm-contact' != $screen->post_type){ //this is not our custom post, so let's exit
          return;
        }
        ....
    }
    

    Another useful attribute of the WP_Screen object returned by this function is the $screen->base attribute which is set to 'post' for any custom post edit screen, and is set to 'edit' for the custom post admin table page, which is very handy when loading custom css/scripts for additional function on those pages.

  3. Skip to note content
    Contributed by Rami Yushuvaev
    PAGE               $SCREEN_ID           FILE
    -----------------  -------------------  -----------------------
    Media Library      upload               upload.php
    Comments           edit-comments        edit-comments.php
    Tags               edit-post_tag        edit-tags.php
    Plugins            plugins              plugins.php
    Links              link-manager         link-manager.php
    Users              users                users.php
    Posts              edit-post            edit.php
    Pages              edit-page            edit.php
    Edit Site: Themes  site-themes-network  network/site-themes.php
    Themes             themes-network       network/themes
    Users              users-network        network/users
    Edit Site: Users   site-users-network   network/site-users
    Sites              sites-network        network/sites
    
  4. Skip to note content
    Contributed by Codex

    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', 'wpdocs_admin_add_page');
    
    /**
     * Add an admin page
     */
    function wpdocs_admin_add_page() {
    	global $wpdocs_admin_page;
    	$wpdocs_admin_page = add_options_page(__('Wpdocs Admin Page', 'wpdocs_textdomain'),
    		__('Wpdocs Admin Page', 'wpdocs_textdomain'),
    		'manage_options', 'wpdocs_textdomain', 'wpdocs_admin_page');
    
    	// Adds my_help_tab when my_admin_page loads
    	add_action('load-'.$wpdocs_admin_page, 'wpdocs_admin_add_help_tab');
    }
    
    /**
     * Add a contextual help tab to the Wpdocs Admin Page
     */
    function wpdocs_admin_add_help_tab () {
        global $wpdocs_admin_page;
        $screen = get_current_screen();
    
        /*
         * Check if current screen is Wpdocs Admin Page
         * Don't add help tab if it's not
         */
        if ( $screen->id != $wpdocs_admin_page )
            return;
    
        // Add my_help_tab if current screen is My Admin Page
        $screen->add_help_tab( array(
            'id' => 'wpdocs_help_tab',
            'title' => __('Wpdocs Help Tab'),
            'content' => '<p>'
    		. __( 'Descriptive content that will show in Wpdocs Help Tab body goes here.', 'wpdocs_textdomain' )
    		. '</p>',
        ) );
    }
    ?>
    
    

You must log in before being able to contribute a note or feedback.