Add A Setting Page To Custom Post Type

Add A Custom Post Type Admin Menu

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]<?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’);
}

?>[/php]

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.

Reference

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s