WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Is there an alternative function to the globals $menu, $submenu? (5 posts)

  1. Frank Staude
    Member
    Posted 1 year ago #

    Hello,

    I need for a plugin the menu / submenu structure of the backend.
    The only thing I have found the global variable $ menu and $ submenu. I would however like to avoid global variables. Is there a function or class that provides me with the menu structure from the backend?

    many greetings
    frank

  2. bcworkz
    Member
    Posted 1 year ago #

  3. Frank Staude
    Member
    Posted 1 year ago #

    Hello bcworkz,

    it reveals itself to me not know what you want out. On http://codex.wordpress.org/Administration_Menus # Function_Reference I can find any function that allows me to read the menu structure. On the contrary, at the point 'position' is even referred to the Global -. "To see the current menu positions, use print_r ($ GLOBALS ['menu']) after the menu has loaded"

    I want to select the backend menu and would like to dispense with the global variable.

    Currently (with global variable) I solve it this way:

    function xx() {
      global $ menu, $ submenu;
      foreach ($ menu as $ menuentry) {
        // Do something
        if (is_array ($ submenu [$ menuentry [2]])) {
          foreach ($ submenu [$ menuentry [2]] as $ submenuentry) {
          // Do something
          }
        }
      }
    }

    But I am not happy with the global variable and would like prefer a "nicer" solution. Therefore, the question is how the WordPress way to read out the menu structure.

    Greetings
    frank

  4. bcworkz
    Member
    Posted 1 year ago #

    Frank, I think I now understand your question better. I agree with your disdain of globals, but unfortunately, WordPress makes extensive use of them for many different purposes. There is no "nicer" class object or similar structure underlying the backend menus. The variable arrays you have identified is all there is.

    Hard to believe, I know. You can see for yourself, all related files are in the wp-admin folder. The default menu arrays are defined in menu.php and includes/menu.php. menu-header.php is responsible for displaying the menus, in which you can see only the globals are used, nothing else.

    There is a reason for this madness. Any arbitrary backend page can easily access, display or manipulate the menu with out having to rely on the references being passed or itself being within the proper scope to use "nicer" structures. Crude, but useful.

    Cheers

  5. Frank Staude
    Member
    Posted 1 year ago #

    Ok, I have to accept the fact that as I have to do as I already do.

    Frank

Topic Closed

This topic has been closed to new replies.

About this Topic