• I’m writing a plugin that adds a top-level menu, along with a set of submenus for it.

    I’m adding the pages like this:

    add_menu_page('Top Level', 'Top Level', 'read', __FILE__, 'my_admin_overview_function');
    add_submenu_page(__FILE__, 'Diagnostics', 'Diagnostics', 'manage_options', 'my_admin_diagnostics', 'my_admin_diagnostics_function');

    Ideally, this would then produce a submenu to the top-level menu where the first tab is called “Overview”, second tab called “Diagnostics”. However, it seems that the first submenu tab is “Top Level” and there appears to be no way to change this, unless I start fiddling with the $submenu variable directly. Is there a better way to do this?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator Samuel Wood (Otto)

    (@otto42)

    www.ads-software.com Admin

    Shouldn’t you make that first line into this then?

    add_menu_page('Top Level', 'Overview', 'read', __FILE__, 'my_admin_overview_function');

    I’m not certain that’s it, as I’ve never tried this sort of thing, but that seems to be how it’s supposed to work to me. I might be wrong, that code is somewhat confusing.

    Thread Starter sargant

    (@sargant)

    The first argument gives the component that goes into the “breadcrumb trail”-type-title for the window, the second gives the menu tab name. It’s the second one that is being used for both.

    I’ve had a poke about in the source and it appears this behaviour is intented, the tab name is assigned to the first you create a subpage.

    I’m wondering whether to file it as a bug report for enhancement, but last time I did that I was informed I had badly overlooked the codex.

    chiggins

    (@chiggins)

    I just had this problem and figured it out, there’s a comment in the add_submenu_page function that says that if it’s adding a submenu to a top-level menu that doesn’t already have one, it’ll add the top-level as a submenu tab. I don’t get that either. The other problem I was having was with having the submenu generation in the same file as the top-level menu, which evidently isn’t good either.

    The work around I ended up with was to:

    1. call the add_submenu_page() functions before the add_menu_page() function, and
    2. have each submenu page in its own file rather than trying to put a generation function in the top-level menu’s file

    The last part bothers me, because the Codex explicitly says that it’s better to use the optional function arg in each case, but I couldn’t get it to work that way.

    Dunno if that helped at all?

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘add_menu_page creating own submenu page’ is closed to new replies.