Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 245 Vote(s) - 3.47 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Wordpress add submenus to custom menu

#1
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!
Reply

#2
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:

[To see links please register here]


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!
Reply

#3
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");
Reply

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


Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through