2

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.

4
  • I was writing an admin menu some time ago and had to work out how pages were determined myself. I'm not sure if this code is from an older version, it's from a backup, and i'm not sure if i have more recent code somewhere, but here's a pastebin of some code i played around with(pretty sure i have a more recent copy somewhere but this is all i could find by quickly looking, so hope it's helpful).
    – t31os
    Commented Nov 29, 2011 at 20:19
  • There's a missing JS file to go with that code but i don't seem to have a backup of it. I know i have a more recent copy uploaded on a testing website(though how much it differs i don't know), so i can always go grab that if necessary, just pointing out that the code posted is incomplete, figured you might appreciate taking a look at it all the same.
    – t31os
    Commented Nov 29, 2011 at 20:21
  • Oh, lol, there may also be some test stuff in there, along with some data i collected and commented into the end of the file(i didn't check what i was pasting before i did it).. so feel free to discard anything not relevant..
    – t31os
    Commented Nov 29, 2011 at 20:25
  • @t31os Hey, thanks for your comments (should be an answer - I'd appreciate to up vote that effort). Basically your menu loop (using the menu/submenu, skipping the separators) is pretty much the same. If you have an e-mail address, I'd mail to you. So far my biggest problem is the following Q: "How would I retrieve the screen IDs of all admin UI screens at one time?". Solving this would allow me to check against the array of screen IDs and identify the current page. Hopefully this also allows a ticket for a better and easier admin menu.
    – kaiser
    Commented Nov 30, 2011 at 2:59

2 Answers 2

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.

0

Since we now have get_current_screen(), which returns a nice object, it's become fairly easy to just use that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.