Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WordPress | Adding a custom post menu under custom menu in the admin

I am trying to create a plugin, with a Dashboard page, config page, some extra pages for configuration and some custom post types.

More specific, in the admin I like to have some pages added in the menu via the functions add_menu_page and add_submenu_page, as well I like to create some custom post types related to the plugin.

The question is, how to group the custom post types menus under the plugin menu options.

In example, lets say I creating the menu option "MyPlugin" with the function add_menu_page and then below this menu I adding the pages "Settings Page", "Do stuff page", "Dashboard" via the function add_submenu_page and then I create the custom post type "Cars". How can I place the "Cars" menu under the MyPlugin menu option ?

The final result I like to be like that :

Dashboard
    Home
    ...
Posts
    All Posts
    ...
Settings
    General
    ...
    ...
MyPlugin             <- How to add this menu structure ?
    Dashboard        <- How to add this menu structure ?
    Cars             <- How to add this menu structure ?
    Settings Page    <- How to add this menu structure ?
    Do stuff page    <- How to add this menu structure ?

The actual issue is not how to create the menu structure, but how to add the "Cars" custom post type menu under the MyPlugin menu.

Note I have try the following option in the "register_post_type" attributes with no luck

'show_in_menu'          =>  'admin.php?page=myplugin.php'

Is that posible to achived ?

like image 303
KodeFor.Me Avatar asked Sep 16 '25 23:09

KodeFor.Me


1 Answers

I had the same problem and I proved this solution:

for your plugin menu page try this:

function my_plugin_menu(){
   add_menu_page( 
       'My Plugin', 
       'My Plugin', 
       'capabilities', 
       'my_plugin_index', 
       'my_plugin_function', 
       plugins_url( 'images/my_plugin_icon.png', __FILE__ ), 
       menu_position 
    );
// add some submenu pages
   ...
   ...
}

in your register_post_type function this:

'show_in_menu' => 'my_plugin_index' // slug from your plugin menu page

this show your menu page in the position that you choose and as submenu the custom post type.

like image 169
Pablo Macias Avatar answered Sep 19 '25 16:09

Pablo Macias