0

I am trying to display the Dashboard's menu by showing the name and the filename associated to it like so:

Dashboard > index.php
Posts > edit.php
Media > upload.php
etc...

This is what I have so far with using the global $menu to show all of the available menu items on the Dashboard:

global $menu;
foreach ( $menu as $group => $item ) {
    foreach ( $item as $label ) {
        echo $label . " > " . $item[2] . "<br>";
    }
    echo "<br>";
}

However, it gives the following result:

Dashboard > index.php
read > index.php
index.php > index.php
> index.php
menu-top menu-top-first menu-icon-dashboard menu-top-last > index.php
menu-dashboard > index.php
dashicons-dashboard > index.php

> separator1
read > separator1
separator1 > separator1
> separator1
wp-menu-separator > separator1

Posts > edit.php
edit_posts > edit.php
edit.php > edit.php
> edit.php
menu-top menu-icon-post open-if-no-js menu-top-first > edit.php
menu-posts > edit.php
dashicons-admin-post > edit.php

etc...

I see that I am able to get Dashboard > index.php to show, but then there is extra information that I do not need. Any recommendation on what I need to change to just display the first two items of each menu?

2

Not sure what you are trying to accomplish precisely, since you don't cover that part.

Looking at the data I would guess you can filter out separators and such by checking for proper title:

global $menu;
foreach ( $menu as $group => $item ) {

    if ( ! empty( $item[0] ) ) {
        echo $item[0] . " > " . $item[2] . "<br>";
    }
}

Results for me in:

Dashboard > index.php
Posts > edit.php
Media > upload.php
Pages > edit.php?post_type=page
Comments 0 > edit-comments.php
Test > edit.php?post_type=test
Appearance > themes.php
Plugins 0 > plugins.php
Users > users.php
Tools > tools.php
Settings > options-general.php
SEO 00 notifications > wpseo_dashboard
1
  • Thank you Rarst, the purpose is to display all of the menu items as checkboxes in my plugin settings. I was testing out the results by printing them to make sure I got the logic down. Aug 16 '16 at 18:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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