12

When I use add_menu_page & add_submenu_page to add menu items,

add_menu_page( 'Forms', 'Forms', 'administrator', 'forms', 'forms_job_menupage_cb' );
add_submenu_page( 'forms', 'Job Applications', 'Job Applications', 'administrator', 'job-applications', 'forms_job_menupage_cb' );
add_submenu_page( 'forms', 'Quote Requests', 'Quote Requests', 'administrator', 'quote-req', 'forms_req_menupage_cb' );
add_submenu_page( 'forms', 'Contact', 'Contact', 'administrator', 'contact', 'forms_contact_menupage_cb' );

I will get something like

  • Forms
    • Forms
    • Job Applications
    • Quote Requests
    • Contacts

Is it possible to create it such that it becomes

  • Forms
    • Job Applications
    • Quote Requests
    • Contacts

In other words Forms will link to Job Applications and I dont want the extra Forms submenu item

13

Hi @JM at Work:

Yes, it is unfortunately that the submenu page is added for every menu page. It would be nice if there were an option but alas, there currently is not.

To remove the submenu page option in WordPress 3.1 or great use remove_submenu_page() with code like this in your theme's functions.php file, or in a .php file of a plugin you might be writing:

add_action( 'admin_menu', 'yoursite_admin_menu' );
function yoursite_admin_menu() {
  add_menu_page( 'Forms', 'Forms', 'administrator', 'forms', 'forms_job_menupage_cb' );
  add_submenu_page( 'forms', 'Job Applications', 'Job Applications', 'administrator', 'job-applications', 'forms_job_menupage_cb' );
  add_submenu_page( 'forms', 'Quote Requests', 'Quote Requests', 'administrator', 'quote-req', 'forms_req_menupage_cb' );
  add_submenu_page( 'forms', 'Contact', 'Contact', 'administrator', 'contact', 'forms_contact_menupage_cb' );
  remove_submenu_page('forms','forms');
}

If you are still using WordPress 3.0 you have to unset() an element of the global variable $submenu like this:

add_action( 'admin_menu', 'yoursite_admin_menu' );
function yoursite_admin_menu() {
  add_menu_page( 'Forms', 'Forms', 'administrator', 'forms', 'forms_job_menupage_cb' );
  add_submenu_page( 'forms', 'Job Applications', 'Job Applications', 'administrator', 'job-applications', 'forms_job_menupage_cb' );
  add_submenu_page( 'forms', 'Quote Requests', 'Quote Requests', 'administrator', 'quote-req', 'forms_req_menupage_cb' );
  add_submenu_page( 'forms', 'Contact', 'Contact', 'administrator', 'contact', 'forms_contact_menupage_cb' );
  unset($GLOBALS['submenu']['forms'][0]);
}
1
3

Assign the first submenu slug similar to the parent menu. In your case, replace 'job-applications' with the slug of the main menu 'forms'.

add_menu_page( 'Forms', 'Forms', 'administrator', 'forms', 'forms_job_menupage_cb' );
add_submenu_page( 'forms', 'Job Applications', 'Job Applications', 'administrator', 'forms', 'forms_job_menupage_cb' );
add_submenu_page( 'forms', 'Quote Requests', 'Quote Requests', 'administrator', 'quote-req', 'forms_req_menupage_cb' );
add_submenu_page( 'forms', 'Contact', 'Contact', 'administrator', 'contact', 'forms_contact_menupage_cb' );
1

I had to add_submenu_page, then remove it (wp 4.0.1)

add_menu_page( 'Admin Tools', 'Admin Tools', 'manage_options','parent', '__return_null','', "70.23423" );
    // this 'hides' the extra.  actually, just makes the text nothing: ''  
    add_submenu_page( 'parent', '',     '', 'manage_options',   'parent',   '__return_null' );
    //ah, but this removes it completely (you need to add it, then remove it :/     
    remove_submenu_page('parent','parent');
1

I would suggest keeping the extra menu item, becuase it's how all the other WP menus work. (I know that's not what you asked!...) The way the other top-level menus work is that they have different labels for the big menu title and the duplicate one below it (e.g. 'Users', 'All Users'; 'Tools', 'Available Tools'). I think it's good to stick with the same paradigm, so I usually rename the first submenu item like this:

<?php
add_menu_page( 'Plugin Name', 'Plugin Name', 'read', 'plugin-slug', $callback );
add_submenu_page( 'plugin-slug', 'Plugin Overview', 'Overview', 'read', 'plugin-slug', $callback );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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