Reputation: 4199
I am new to wordpress development. I am trying to create a plugin but my top-level menu item is also being displayed as sub-menu item.below is my code.
<?php
/*
Plugin Name: rooties_main_menu
*/
add_action('admin_menu', 'gpwp_rooties_create_menu');
function gpwp_rooties_create_menu() {
add_menu_page('My Rooties Setting','Rooties Settings', 'manage_options', __FILE__, 'gpwp_rooties_setting_page',plugins_url('/images/wordpress.png',__FILE__) );
add_submenu_page(__FILE__, 'About Rooties Plugin', 'About', 'manage_options',__FILE__.'_about',gpwp_rooties_setting_about_page);
add_submenu_page(__FILE__, 'Today\'s Menu', 'Today\'s Menu', 'manage_options',__FILE__.'_about',gpwp_rooties_setting_menu_form_page);
}
?>
I know this is not the best code as per security prospects. I am trying these at my local system.
As per above code only "About" and "Today's menu" should be displayed as sub-menu but it is also displaying "Rooties Settings" too. Please let me know where i am wrong.
Upvotes: 3
Views: 2176
Reputation: 6777
From the Codex: http://codex.wordpress.org/Adding_Administration_Menus#Using_add_submenu_page
For existing WordPress menus, the PHP file that handles the display of the menu page content. For submenus of a custom top-level menu, a unique identifier for this sub-menu page.
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.
There are some code examples here, although they are old -> http://wordpress.org/support/topic/add_menu_page-always-add-an-extra-subpage
Before:
add_menu_page('Section', 'Section', 10, __FILE__, 'section');
add_submenu_page(__FILE__, 'Edit', 'Edit', 10, 'section-edit', 'section_edit');
Fixed:
add_menu_page('Section', 'Section', 10, __FILE__, 'section');
add_submenu_page(__FILE__, 'Edit', 'Edit', 10, __FILE__, 'section_edit');
Upvotes: 3