I am always trying to remember how to properly add menu pages in the admin area of WordPress. Here is a code snippet that will give you a good start at creating your own admin menu choice with multiple options underneath it. This can sometimes be confusing with the different places menu slugs need to match up and such. In the following code snippet the first piece is adding an action when admin_menu gets generated for the function of mycustomplugin_menu_pages to get called and add this customized menu to the system. This creates the main custom plugin menu item and the first add_submenu_page duplicates this settings page as the first choice in the sub menu list. Then a secondary add_submenu_page adds another page you can use in your custom plugin. The mycustomplugin_settings function gets called by the main menu item and the first sub menu item when those are chosen. The mycustomplugin_second function is what gets called by the second sub menu item. Inside these functions they also are checking to confirm that you have the proper capabilities to access these pages. Even if they are not visible in the admin menu, you could still attempt to access this page by manually entering the url but by doing a current_user_can check allows you to confiirm that they should have access to this admin page.

 

%d bloggers like this: