twitter
    Find out what I'm doing, Follow Me :)
If you have any question, suggestion or article topic for me to write, feel free to contact me through my shout box. ;) Some time I need an idea to write. hehe Hopefully I can help you and share my expertise.

getting an unwanted sub menu item in wordpress when using add_submenu_page

We are currently doing project in wordpress environment. In that project, we deal with customization administration menu which it bring us to seek for menu creation using add_menu_page and add_submenu_page function.

here is properties for that functions

add_menu_page(page_title, menu_title, access_level/capability, file, [function], [icon_url]);
add_submenu_page(parent, page_title, menu_title, access_level/capability, file, [function]); basename(__FILE__)


by the way. when we create submenu using add_submenu_page function, we had unwanted sub menu item appear in the first submenu. this submenu is same as main menu like this..


Menu Tile
-Menu Title
-Sub Menu Title 1
-Sub Menu Title 2


we don't want Menu Title appear as a submenu. After googling, I found that other people also got a same problem. I do a research and debugging and finally I got the solution.


add_menu_page('Menu Title', 'Menu Title', 10, 'main_menu', 'sub_menu_1_callback');
add_submenu_page('main_menu', 'Sub Menu Title 1', 'Sub Menu Title 1', 10, 'main_menu' , 'sub_menu_1_callback');
add_submenu_page('main_menu', 'Sub Menu Title 2', 'Sub Menu Title 2', 10, 'sub_menu' , 'sub_menu_2_callback');


It's pretty simple.. just to make sure the first submenu file name is same as their parent file name. In my case, look at first add_submenu_page function file name, main_menu . I put same name as add_menu_page function file name.


Download Wordpress Submenu Sample Plugin

2 comments:

Andrew Traub said...

How do you add menus and submenus where the functions for the submenus are seperated into seperate files, for example filea.php and function_a, fileb.php and function_b, etc?

napi said...

Hi Andrew
you can use include() function to get use function in filea.php and fileb.php.

for example

include('filea.php'); in you parent page. ( where you put add menu function.

Post a Comment