Add A Setting Page To Custom Post Type
Recently I needed to add a settings page to two custom post types I was using in a new theme. Adding an admin page to one of the default menus in the WordPress control panel is easy. The WordPress Codex has a nice article explaining how to hook sub-menu pages to “posts” “media” “categories” and so on. It’s very useful for plugins and theme authors. One thing the article doesn’t do is explain how to add a sub-menu to a custom post type. Turns out, it’s easy to do. Here’s the code we will use and a brief explanation of it.
<?php
add_action('admin_menu' , 'brdesign_enable_pages');
function brdesign_enable_pages() {
add_submenu_page('edit.php?post_type=custom', 'Custom Post Type Admin', 'Custom Settings', 'edit_posts', basename(__FILE__), 'custom_function');
}
?>
First we add an action for our new admin menu. Once we’ve done that, we create a function for our page(s). Here’s what the function does. First we are saying the place we’d like the menu to go is with the post type we’ve defined called “custom.” That’s what the edit.php?post_type=custom part is dong. Just replace “custom” with the post type that you’ve already defined and the menu will be added there. If you need help registering a custom post type, here’s a great article on the subject by Justin Tadlock.
Our next two options deal with titles and menu names. The ‘Custom Post Type Admin’ is the page title that will appear at the top of your browser page while using the page. The ‘Custom Settings’ is the name that will appear in your menu under your custom post type.
The ‘edit_posts’ set the minimum capabilities a user has to have in order to access this page. If the current user can edit posts then they can access our admin page. The Codex has a list of roles and capabilities you can see to help determine what to place here.
The ‘basename(__FILE__)’ serves as the slug that will go in your URL for the admin page you create. Lastly the ‘custom_function’ is optional and will call any functions you need on your new admin page.
That’s all there is to it. Now you can easily add a settings page to enhance your custom post types.




Bobby said:
on May 25, 2011 at 10:01 am
So I’m looking for a way to kinda do the reverse of this and place custom post types in my plugin.
thought that this would simply do the trick:
‘show_in_menu’ => ‘admin.php?page=plugin_admin’
page=plugin_admin is one of my submenu pages, so I don’t think I’m doing something right.
You have any experience doing this?
Bill Robbins said:
on May 26, 2011 at 11:15 am
That’s one I haven’t tried myself. I took a look at a couple of plugins that use custom post types. Both of those use the post type as the main menu element with supporting settings and taxonomies under it instead of the other way around. The Codex indicates that what you’d like to do is possible:
“Note: When using ‘some string’ to show as a submenu of a menu page created by a plugin, this item will become the first submenu item, and replace the location of the top level link. If this isn’t desired, the plugin that creates the menu page needs to set the add_action priority for admin_menu to 9 or lower. http://codex.wordpress.org/Function_Reference/register_post_type“
It wouldn’t discuss menu order unless it was possible to add a post type to a plugin’s menu section.