WordPress.org

Ready to get started?Download WordPress

Forums

add_menu_page always add an extra subpage (6 posts)

  1. benlag
    Member
    Posted 5 years ago #

    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

  2. benlag
    Member
    Posted 5 years ago #

    Anyone?

  3. Dustin
    Member
    Posted 5 years ago #

    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.

  4. Dustin
    Member
    Posted 5 years ago #

    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.

  5. benlag
    Member
    Posted 5 years ago #

    Hi djboling,

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

    Ben

  6. GeertDD
    Member
    Posted 4 years ago #

    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

Topic Closed

This topic has been closed to new replies.

About this Topic