WordPress add submenus to custom menu

In a wordpress plugin I’m creating, it’s creating a new top-level admin menu with a sub-menu page. Here’s my code:

add_menu_page('Eastview Custom', 'Eastview Custom', 5,"eastview-custom");
add_submenu_page("eastview-custom","GLS Lunch Orders","GLS Lunch",5,'glsLunch','glsLunch');

So this code creates a new admin menu, “Eastview Custom”. Then it adds two sublinks: “Eastview Custom” and “GLS Lunch”. The problem is that I don’t want “Eastview Custom” as a sublink. I would like the only sublink to be “GLS Lunch”. I can’t figure out how to do this. Thanks for any help!

Related posts

Leave a Reply

3 comments

  1. According to the codex

    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_slug and menu_slug parameters being given the same value.

    Which you can see on this page here: http://codex.wordpress.org/Adding_Administration_Menus#Sub-Menus

    So according to the Codex you should be able to have something like the following (note that I’ve replaced your user levels parameter with capabilities as they are deprecated, and standardised it all to single quotes);

    add_menu_page('Eastview Custom', 'Eastview Custom', 'manage_options', 'my-top-level-handle');
    add_submenu_page( 'my-top-level-handle', 'GLS Lunch Orders', 'GLS Lunch', 'manage_options', 'my-top-level-handle');
    

    Now you’d think that this would work based on the Codex – it doesn’t. It won’t display any sub menu items simply because there is only one of them. If you add another item you’ll see that this works, ie;

    add_menu_page('Eastview Custom', 'Eastview Custom', 'manage_options', 'my-top-level-handle');
    add_submenu_page( 'my-top-level-handle', 'GLS Lunch Orders', 'GLS Lunch', 'manage_options', 'my-top-level-handle');
    add_submenu_page( 'my-top-level-handle', 'New Item', 'New item', 'manage_options', 'new-handle');
    

    Hope this helps a bit, shame I couldn’t find the answer to the single list item!

  2. the alternative is you can remove the submenu after create main menu

    add_menu_page('Eastview Custom', 'Eastview Custom', 5,"eastview-custom");
    add_submenu_page("eastview-custom","GLS Lunch Orders","GLS Lunch",5,'glsLunch','glsLunch');
    remove_submenu_page("eastview-custom", "eastview-custom");
    
  3. You can use this function

    function custom_add_admin_menu_page($args = []) {
        $args = wp_parse_args($args, [
            'page_title' => '',
            'menu_title' => '',
            'capability' => 'manage_options',
            'function'   => false',
            'menu_slug'  => '',
            'icon_url'   => '',
            'position'   => 20,
        ]);
       add_menu_page(
            $args['page_title'],
            $args['menu_title'],
            $args['capability'],
            $args['menu_slug'],
            $args['function'],
            $args['icon_url'],
            $args['position']
        );
    
        add_action('admin_head', function () use ($args) {
            if (!$args['function']) {
                
                remove_submenu_page($args['menu_slug'], $args['menu_slug']);
            }
        }, 99);
    
    }
    

    and use like below

    custom_add_admin_menu_page([
        'page_title' => __('your page title', 'your domain'),
        'menu_title' => __('your menu title', 'your domain'),
        'menu_slug'  => 'your slug',
        'icon_url'   => 'dashicons-page',
        'position'   => 20,
    ]);