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 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.