2

I am using this code:

add_menu_page($page_title, $menu_title, $this->capability, $menu_slug, $function);

Which is adding top level admin page. When I add:

add_submenu_page( $menu_slug, 'sub menu 1', 'sub menu 1', $this->capability, $menu_slug . '_sub_menu_page_1', $function );

I get not only the desired child page, but also the parent page moves to become a child page of itself.

4
  • You could try unsetting it from the $submenu var inside a callback hooked onto admin_menu.
    – t31os
    Nov 23, 2011 at 9:57
  • how have you set $menu_slug?
    – rds
    Nov 25, 2011 at 9:07
  • menu_slug and menu_slug_sub_menu_1
    – Mild Fuzz
    Dec 1, 2011 at 19:52
  • Can we see the actual code being used? Dec 2, 2011 at 0:54

5 Answers 5

5
+50

Unless I misunderstand the question, this should do the trick:

add_action('admin_menu', 'add_menu_pages');

function add_menu_pages() {
    add_menu_page('Menu Title', 'Menu Title', 10, 'main_menu', 'submenu_1_callback');
    add_submenu_page('main_menu', 'Sub Menu Title 1', 'Sub Menu Title 1', 10,  'main_menu' , 'submenu_1_callback');
    add_submenu_page('main_menu', 'Sub Menu Title 2', 'Sub Menu Title 2', 10, 'sub_menu' , 'submenu_2_callback');
}

function submenu_1_callback() {
    echo "<h2>" . __( 'Toplevel & Sublevel 1', 'menu-test' ) . "</h2>";
}

function submenu_2_callback() {
    echo "<h2>" . __( 'Sublevel 2', 'menu-test' ) . "</h2>";
}

EDIT:

Here's the easiest way to hide the first sub-menu item.

1) Create admin.css and move it to /[your template dir]/css/

2) Add this to your admin.css:

#adminmenu li#toplevel_page_main_menu li.wp-first-item {
    display: none !important;
}

3) Add the following code to your functions.php

add_action('admin_enqueue_scripts', 'custom_admin_css');
function custom_admin_css() {
    wp_enqueue_style( 'admin_css', (get_template_directory_uri() . '/css/admin.css'), false, '1.0.0' );
}
2
  • no, the problem is the way it displayed on the sidebar. I do not want the main page to be repeated in the sub pages, beneath it.
    – Mild Fuzz
    Dec 1, 2011 at 19:50
  • Edited answer should work. You should realize, however, that when the admin menu is collapsed and you hover over the top-level menu icon, the top-level menu item text becomes a label instead of a link (only the icon stays a link).
    – Paul
    Dec 2, 2011 at 0:46
10

use remove_submenu_page() like this :

add_action('admin_menu','your_add_menu_function');
function your_add_menu_function() {
  add_menu_page('Menu Page Title','Menu Name','edit_theme_options/others caps...','parent_menu_slug',...);
  add_submenu_page('parent_menu_slug','Menu Page Title','Menu Name','...caps...','sub_menu_slug',...);
  add_submenu_page(...);
  remove_submenu_page('parent_menu_slug','parent_menu_slug'); // pay a attention
}

ok, parent_menu links to the first submenu page.

1

The standard trick is to repeat the main menu page as a submenu page, without the menu title (3rd parameter).

function add_menu_pages() {
    add_menu_page('Page Title', 'Menu Title', 'edit_posts', 'main_menu', 'menu_1_callback');
    add_submenu_page('main_menu', 'Page Title', '', 'edit_posts',  'main_menu' , 'menu_1_callback');
    add_submenu_page('main_menu', 'Page Title 1', 'Submenu Title 1', 'edit_posts',  'main_menu' , 'submenu_1_callback');
    add_submenu_page('main_menu', 'Page Title 2', 'Submenu Title 2', 'edit_posts', 'sub_menu' , 'submenu_2_callback');
}

That will give you

Menu Title
Submenu Title 1
Submenu Title 2
0

Is there some reason that you don't just use remove_submenu_page() as applicable to remove the page being added, and then just build (or re-build, as the case may be) your menu structure?

But really, that would be a hack, and the need to use a hack is indicative of something doing wrong that needs to be fixed. For that, we'll probably need the actual code being used.

-1

add_action( 'admin_menu', 'my_admin_menu' );

function my_admin_menu() { add_menu_page( 'Optins', 'Theme Options', 'manage_options', 'options', 'options_admin_page', 'dashicons-admin-generic', 6 ); add_submenu_page( 'options', 'First Sub Options', 'First Option', 'manage_options', 'options', 'options_admin_sub_page_first' ); add_submenu_page( 'options', 'Second Sub Options', 'Second Option', 'manage_options', 'second-option', 'options_admin_sub_page_second' ); } function options_admin_page(){

}

function options_admin_sub_page_first(){

}

function options_admin_sub_page_second(){

}

Now you will get something like it, simple :)

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.