• Hi,

    I’m trying to build a plugin for a client and need to add a separate section to the admin menus.

    I use add_menu_page & add_submenu_page to create the section, like so:

    add_menu_page('Section', 'Section', 10, __FILE__, 'section');
    add_submenu_page(__FILE__, 'Edit', 'Edit', 10, 'section-edit', 'section_edit');

    This does add a new section, but it always creates an extra submenu page with the same details as the parent menu page. So it looks like this:
    Section
    Section
    Edit

    I don’t understand why the Section submenu is automatically added – it’s not in line with the rest of the admin section – how can I prevent that?

    Thanks
    Ben

Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter benlag

    (@benlag)

    Anyone?

    I’d be very interested to learn about a solution to this as well. I find it a bit irritating. Maybe its a little OCD, but I really can’t stand the redundancy.

    Ok, I found the declaration of the add_menu_page function and found that this is how you can change your code:

    Was:

    add_menu_page('Section', 'Section', 10, __FILE__, 'section');
    add_submenu_page(__FILE__, 'Edit', 'Edit', 10, 'section-edit', 'section_edit');

    Fixed:

    add_menu_page('Section', 'Section', 10, __FILE__, 'section');
    add_submenu_page(__FILE__, 'Edit', 'Edit', 10, __FILE__, 'section_edit');

    I personally use basename(__FILE__), so it would look like:

    add_menu_page('Section', 'Section', 10, basename(__FILE__), 'section');
    add_submenu_page(basename(__FILE__), 'Edit', 'Edit', 10, basename(__FILE__), 'section_edit');

    Hope this helps.

    Thread Starter benlag

    (@benlag)

    Hi djboling,

    Thanks for the help. That does fix the problem, nice one.

    Ben

    From the manual:

    In situations where a plugin is creating its own top-level menu, the first submenu will normally have the same link title as the top-level menu and hence the link will be duplicated. The duplicate link title can be avoided by calling the add_submenu_page function the first time with the parent and file parameters being given the same value.

    http://codex.wordpress.org/Adding_Administration_Menus#Sub-Menus

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘add_menu_page always add an extra subpage’ is closed to new replies.