How to determin which admin screen/sub screen I’m currently viewing

Edit: The Q narrowed down to the following:

“How would I retrieve the screen IDs of all admin UI screens at one time?”


Edit: I added a bunch of more plugin globals.


Q is the following: Check which AdminUI submenu page/screen you’re viewing and check this against some global var or some internal function.

// This helps you inspecting the data - just drop it in some functions.php file of a theme
function wpse_inspect_admin_screen_data()
{
    echo '<pre>';
        echo '<h3>global <code>$menu</code></h3>';
        print_r( $GLOBALS['menu'] );
        echo '<hr />';

        echo '<h3>global <code>$submenu</code></h3>';
        print_r( $GLOBALS['submenu'] );
        echo '<hr />';

        echo '<h3>global <code>$self</code></h3>';
        print_r( $GLOBALS['self'] );
        echo '<hr />';

        echo '<h3>global <code>$parent_file</code></h3>';
        print_r( $GLOBALS['parent_file'] );
        echo '<hr />';

        echo '<h3>global <code>$submenu_file</code></h3>';
        print_r( $GLOBALS['submenu_file'] );
        echo '<hr />';

        echo '<h3>global <code>$plugin_page</code></h3>';
        print_r( $GLOBALS['plugin_page'] );
        echo '<hr />';

        echo '<h3>global <code>$admin_page_hooks</code></h3>';
        print_r( $GLOBALS['admin_page_hooks'] );
        echo '<hr />';

        echo '<h3>global <code>$_parent_pages</code></h3>';
        print_r( $GLOBALS['_parent_pages'] );
        echo '<hr />';

        echo '<h3>global <code>$_registered_pages</code></h3>';
        print_r( $GLOBALS['_registered_pages'] );
        echo '<hr />';

        echo '<h3>global <code>$title</code></h3>';
        print_r( $GLOBALS['title'] );
        echo '<hr />';

        echo '<h3>global <code>$_wp_real_parent_file</code></h3>';
        print_r( $GLOBALS['_wp_real_parent_file'] );
        echo '<hr />';

        echo '<h3>global <code>$_wp_menu_nopriv</code></h3>';
        print_r( $GLOBALS['_wp_menu_nopriv'] );
        echo '<hr />';

        echo '<h3>global <code>$_wp_submenu_nopriv</code></h3>';
        print_r( $GLOBALS['_wp_submenu_nopriv'] );
        echo '<hr />';

        // built by set_current_screen() in /core_root/wp-admin/includes/template.php line 2085 (wp 3.2.x)
        echo '<h3>global <code>$current_screen</code></h3>';
        print_r( $GLOBALS['current_screen'] );
    echo '<pre>';
}
add_action( 'shutdown', 'wpse_inspect_admin_screen_data', 9999 );

I already have an Array containing the slug and Label from both the parent and sub menu item. Here you can see an exemplary part of the array I built from global $menu, $submenu; and that I need to check against:

Array
(
    [Appearance] => Array
        (
            [0] => Array
                (
                    [label] => Themes
                    [slug] => themes.php
                    [parent_label] => Appearance
                    [parent_file] => themes.php
                )
            [1] => Array
                (
                    [label] => Widgets
                    [slug] => widgets.php
                    [parent_label] => Appearance
                    [parent_file] => themes.php
                )
            [2] => Array
                (
                    [label] => Editor
                    [slug] => theme-editor.php
                    [parent_label] => Appearance
                    [parent_file] => themes.php
                )
        )
    [Pages] => Array
        (
            [0] => Array
                (
                    [label] => All Pages
                    [slug] => edit.php?post_type=page
                    [parent_label] => Pages
                    [parent_file] => edit.php?post_type=page
                )
            [1] => Array
                (
                    [label] => Add New
                    [slug] => post-new.php?post_type=page
                    [parent_label] => Pages
                    [parent_file] => edit.php?post_type=page
                )
        )
)

Note: The Q is not the loop. Only what exactly I can use to check which menu item and subitem I’m currently watching.

2 Answers
2

Since admin menu itself knows how to display where you are at, it is reasonable to look at code responsible for that logic.

Menu output is generated by _wp_menu_output() in menu-header.php. It makes use of number of global variables, namely:

global $self, $parent_file, $submenu_file, $plugin_page, $pagenow, $typenow;

The actual logic is quite extensive and naturally differs for native and plugin pages. Look for assignments of 'current' to CSS classes and around.

Leave a Comment