Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

The add_menu_page documentation says to pass the menu title as the second parameter:

add_menu_page('Page Title', 'Menu Title', ...);

When adding more pages later via add_submenu_page, the main page becomes the first entry in the submenu:

enter image description here


However, I want the first item in the list to have a different name (but still point to the same page), the way Wordpress itself does it:

enter image description here


How could I accomplish that in my plugin?

share|improve this question
    
You can accomplish that. But the point is, that every other plugin as well as core will behave different. What do you think your users might expect from yours? The same? Something "new and fresh and uncommon"? –  kaiser Sep 27 '12 at 23:32
    
@kaiser - The way my plugin is set up, this is what the user would expect (similar to how you expect the main "Posts" page to be the "All Posts" sub-menu page). –  MegaHit Sep 27 '12 at 23:35
    
Sorry, completely missunderstood your request. Thought you want to point the to different pages. If you want to know more about handling those items then pleas read my article here. –  kaiser Sep 27 '12 at 23:50

2 Answers 2

up vote 21 down vote accepted

You can make the 'slug' for the submenu page equal that of the top level page, and they'll point to the same place:

add_action('admin_menu', 'my_menu_pages');
function my_menu_pages(){
    add_menu_page('My Page Title', 'My Menu Title', 'manage_options', 'my-menu', 'my_menu_output' );
    add_submenu_page('my-menu', 'Submenu Page Title', 'Whatever You Want', 'manage_options', 'my-menu' );
    add_submenu_page('my-menu', 'Submenu Page Title2', 'Whatever You Want2', 'manage_options', 'my-menu2' );
}
share|improve this answer
    
This would result in duplicate entries, since Wordpress automatically creates a submenu page for the main page. –  MegaHit Sep 27 '12 at 23:37
    
I just tested it, it works, no duplicates. –  Mike Lewis Sep 27 '12 at 23:38
    
I remember skimming through the core code last week and running into a note that said something like: "Add default submenu item IF USER HASN'T ALREADY". It checks to see if there's a submenu item pointing to the top level item. If there is, it doesn't add the default. –  Mike Lewis Sep 27 '12 at 23:40
    
I just tested it, and you're absolutely right. Works like a charm. Thanks. –  MegaHit Sep 27 '12 at 23:41

Simply add this:

$submenu['my-menu'][0][0] = 'My New Menu Title';

For debugging purposes, you can do a print_r($menu) to check the whole WP menu.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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