Anything else you'd like to mention about your project?

Registering a Custom Post Type Tutorial in WordPress

Custom Post Types

WordPress introduced custom post types in WordPress 3.0. That seems not too long ago since we’re on version 4.3 now, but version 3.0 (dubbed Thelonious Monk) was back on June 17, 2010. So this feature has been around for over 5 years. The codex has a pretty decent post about it here. Read up a little if you need to get a little more familiar with it before continuing on to the “Getting Started with this Custom Post Type Tutorial” heading. You can also go here as well to learn about the function that registers your post type.

Getting Started with this Custom Post Type Tutorial

As always whenever you’re editing something that could potentially break your WordPress site, you’ll want to make a back-up of the file. I have read in many different places that it’s a good idea to put your custom post type registrations in a plugin, so that if you switch themes for whatever reason, you’ll still have access to all of your data. I might write a tutorial on how to do that a little later on.

For this custom post type tutorial we are going to assume you’re using functions.php just make sure you, like I said, make a backup of it before continuing. You also might want to enable debug mode in your wp-config.php as well. If you’re unfamiliar with that concept, just go to wp-config.php, commonly in your root directory, and switch define('WP_DEBUG', false); to define('WP_DEBUG', true); and you’ll be set. Don’t do this if you’re in a production environment or if you are uncomfortable debugging PHP, because you might get an error message you won’t know what to do with (although you can always Google it). Error logs are also your best friend for debugging new features like this.

WordPress Post Type Labels

I’m going to just steal a page from the Codex to help explain what each of the labels are, because it does a pretty good job:

‘name’ – general name for the post type, usually plural. The same as, and overridden by $post_type_object->label

‘singular_name’ – name for one object of this post type. Defaults to value of ‘name’.

‘menu_name’ – the menu name text. This string is the name to give menu items. Defaults to a value of ‘name’.

‘name_admin_bar’ – name given for the “Add New” dropdown on admin bar. Defaults to ‘singular_name’ if it exists, ‘name’ otherwise.

‘all_items’ – the all items text used in the menu. Default is the value of ‘name’.

‘add_new’ – the add new text. The default is “Add New” for both hierarchical and non-hierarchical post types. When internationalizing this string, please use a gettext context matching your post type. Example: _x('Add New', 'product');

‘add_new_item’ – the add new item text. Default is Add New Post/Add New Page

‘edit_item’ – the edit item text. In the UI, this label is used as the main header on the post’s editing panel. The default is “Edit Post” for non-hierarchical and “Edit Page” for hierarchical post types.

‘new_item’ – the new item text. Default is “New Post” for non-hierarchical and “New Page” for hierarchical post types.

There you have it, all the labels available to WordPress and what they mean. You might be asking yourself, “That’s great, but how do I implement that?” Well, scroll down to the next part of this lovely custom post type tutorial to find out.

You will notice that I have some PHP commenting in there. This is pretty important stuff to include so you can remember not only where things are, but what the code is doing. This is especially vital in a collaborative development environment, good documentation of your functions and methods are very important.

Next I wanted to point out how we’re using the labels. We have set up an array of label options, and since the label key => value pair is able to take an array as a value, this works well. In fact, you’ll notice that taxonomies, rewrite, and supports also take an array, even if you only have one item in there.

Wrapping Up

There you have it, it’s pretty self-explanatory, and you can get even more robust with your post types. I generally like to customize the “add new post” button label, as well as the trash message. I mainly like customizing the emptied trash message because I like including Easter Eggs.

What kinds of things are your go-to’s when registering post types? Did I leave anything out in my custom post type tutorial that you would like to add? Go ahead and leave these in the comments, and I’ll take a look!