This is a pretty advanced post, but it was causing me issues for about half an hour, so I thought I’d share quickly.
Here’s the deal: I wanted to add a submenu to an existing custom post type. Right away, when creating a custom post type with
show_ui set to
true, WordPress will create a new top-level menu item, with a submenu item of “Add
Already in the plugin, I had created a custom post type:
register_post_type( ‘tdd_beans’, ‘tdd_beans_register_post_type’ );
The details of my beans post-type are unimportant. What is important is the handle. In all cases, it’s the first parameter that you pass to
register_post_type() — in this case,
add_submenu_page( ‘edit.php?post_type=tdd_beans’, ‘Beans Settings’, ‘Settings’, ‘manage_options’, ‘tdd-beans-settings-menu’, ‘tdd_beans_view_settings’ );
add_action( ‘admin_menu’, ‘tdd_pb_beans_menu’ );
add_submenu_page() function takes these parameters:
$parent_slug: The magic is that it requires that whole edit.php whatnot in order to work. At least right now, although I believe that may be a bug in WordPress. Future versions may only require the slug (
tdd_beans). In any case, it’s the slug for the parent page
$page_title: This is what goes between the
$menu_title: What shows up in the menu (keep it short)
$capability: This maps to a WordPress capability that will be required to access this sub-menu. It should probably map to whatever the capability is for your custom post type. Sub-menus will continue to display even if their parent menu is unavailable due to permissions
$menu_slug: Put your own menu slug here for referencing this menu item elsewhere.
$function: Lastly, this is the function that will echo stuff to the page.
echo ‘<div class="wrap"><h2>Hello World (inc. beans)</h2></div>’;