3
votes

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 ?

2

2 Answers

5
votes

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.

4
votes

Within your register_post_type, add this little snippet

"menu_position" => 100,// below second seperator

here are the locations for the menu positions

 2 Dashboard
 4 Separator
 5 Posts
 10 Media
 15 Links
 20 Pages
 25 Comments
 59 Separator
 60 Appearance
 65 Plugins
 70 Users
 75 Tools
 80 Settings
 99 Separator

if you set the position to be greater than 100 it will continue to add them to the very bottom of the admin menu.

I created a custom wp post type generator (SITE GONE), you can see the position on the custom post type on the right hand side, Enter a post type Name, (plural) then under this you can select the menu position, it will change the position to show where this will appear,

if you have trouble building your menu structure try this

function myplugin_menu() {
    add_menu_page('MyPlugin', 'MyPlugin', 'add_users', __FILE__, 'myplugin-page-name', plugins_url('MyPluginFolder/images/icon.png') );
    add_submenu_page(__FILE__, 'Cars', 'Cars', 8, 'myplugin-cars-page', 'cars');
    add_submenu_page(__FILE__, 'Settings', 'Settings', 8, 'myplugin-settings-page', 'myplugin_settings_function');
    add_submenu_page(__FILE__, 'Do Stuff', 'Do Stuff', 8, 'myplugin-dostuff-page', 'myplugin_dostuff_function');
}
add_action('admin_menu', 'myplugin_menu');

http://codex.wordpress.org/Function_Reference/add_menu_page