7

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!

3 Answers 3

9

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!

Sign up to request clarification or add additional context in comments.

6 Comments

OK, thank you. Sorry I didn't reply earlier. That's fine - on the website I'm using this on, there will be more than one submenu page. I just wanted to know how to remove the duplicate link. Thanks!
Great that worked for me !, I had a hard time looking in the Wordpress documentation. Thank you.
No worries! Glad to hear it's still helping ;)
Mind blowing answer. Exactly what I was looking for. Thank you very much.
Nice one @iSaumya, glad it helped.
|
1

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");

2 Comments

I just found my old answer to this question whilst googling, as I had the same problem again (always a bit of a surprise to find your own answer :)). Your answer should be the accepted answer to this question as it actually solves the problem. Nice one!
@McNab haha, sounds funny. Glad if it help
0

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,
]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.