Creating a Menu with Submenus for WordPress Plugins
I want to show an example of code that I myself recently found and I think it will be a good addition to the lesson “Create plugin and admin panel menu with add_menu_page“. Presented below code snippet add menu with submenu. You can use this code as for plugin development, so to develop your own WordPress themes.
To test a piece of code, go to your test site in the template folder, to file functions.php paste the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
function theme_options_panel(){ add_menu_page('Theme page title', 'Theme menu label', 'manage_options', 'theme-options', 'wps_theme_func'); add_submenu_page( 'theme-options', 'Settings page title', 'Settings menu label', 'manage_options', 'theme-op-settings', 'wps_theme_func_settings'); add_submenu_page( 'theme-options', 'FAQ page title', 'FAQ menu label', 'manage_options', 'theme-op-faq', 'wps_theme_func_faq'); } add_action('admin_menu', 'theme_options_panel'); function wps_theme_func(){ echo '<div class="wrap"><div id="icon-options-general" class="icon32"><br></div> <h2>Theme</h2></div>'; } function wps_theme_func_settings(){ echo '<div class="wrap"><div id="icon-options-general" class="icon32"><br></div> <h2>Settings</h2></div>'; } function wps_theme_func_faq(){ echo '<div class="wrap"><div id="icon-options-general" class="icon32"><br></div> <h2>FAQ</h2></div>'; } |
After pasting the code, you will have menu Theme menu label and three sub menu Theme menu label, Settings menu label, FAQ menu label. I think further, if you read the section “For plugin development”, it will not be hard to figure out where to insert the code for each menu item.
/*
- Basic web design course;
- Site layout;
- General course on CMS WordPress and continuation of the course on template development;
- Website development in PHP.