7 days of WordPress plugins, themes & templates - for free!* Unlimited asset downloads! Start 7-Day Free Trial
FREELessons: 10Length: 1.1 hours

Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

2.2 Registering a Custom Post Type

In this lesson, we'll create a plugin that registers a custom post type.

Related Links

2.2 Registering a Custom Post Type

Hello, and welcome back to this Tuts+ course on custom content types in WordPress. In this part of the course, we're gonna start writing some code. And I'll show you how to register a custom post type using a plugin. So here's my plugin, now you can see that the location for my plugin is the plugins directory in the wp-content folder in my site. Within that, I've created a folder called tutsplus_content_types. And my file is called tutsplus-register-post-type.php. Now you wouldn't normally code your plugin directly into your site like this when the file is already uploaded. You should really create the file locally and then test it locally and upload it. So at the beginning, we have the opening commented-out text with the name of our plugin, its URI and description. The version number, which here I've used the same number as the part of the course that we're on, the author, the author URI, the text domain, and the license. So let's start writing our function. Now this function is going to be called tutsplus_register_post_type. And that will need to be hooked to the init hook. So I'm using add_action, and then my first parameter is init, which is the hook. And my second parameter is the name of my function. Now that won't do anything just yet because we haven't actually added anything to our function, so let's start. So the first thing I'll do is define the labels for my post_type, and I'm gonna call my post_type Cities. So I'm gonna create a post type for cities, and then a taxonomy for countries that will apply to each city. So my first label is the name of my post type, which is Cities. And then I have a singular name, and I'm going to copy this so that I don't have to keep typing the same thing out again and again. So this is city. I'm gonna add another tab here to line everything up, and that's the singular name of city. And then I need to add a bunch of other labels, which I'll do quickly. Okay, so those are my labels, and then I'll add my semicolon after my brackets there, after my closing bracket. And then I need to go on to write the arguments. Now if you need to check what these are, the Codex page on the register_post_type function lists out all the labels you can use and all the arguments you can use as well. Some of these default to true or false, and you don't need to actually specify them if you want them to stay at the default. But if your post types are ever not working exactly as you want them to, check out this Codex page. And you'll be able to identify any arguments you need to add. So again, I'm creating an array. So firstly, my labels, So the labels are these array labels up here. Has_archive, I want that to be true so I can have an archive page for my cities. Public will also be true. So here I'm overwriting a default. Hierarchical, now I like to make my custom post types hierarchical, purely because I prefer the user interface for hierarchical post types than I do for non-hierarchical ones. And then we list out which features it supports, and that's in an array. Making sure I don't mess up my punctuation. So all of these have to be in inverted commas. So that's my support, the capability type, Is post, which means that if somebody has the capability of editing posts, an admin or an editor or an author. So somebody has capabilities relating to posts, they will be able to use this particular post type. Rewrite is the slug that will be used for the pages, the individual posts and the archive pages for this post type. And I'm using this because I'm not going to give it a pretty name. I'm gonna use a prefix in the name for my post type, and it would default to that as the slug, so I want to give it a pretty slug. And then finally, and this is something that's quite new, show_in_rest. And what this does is ensure that if you're using an up to date version of WordPress with Gutenberg, you will be able to access this particular post type through Gutenberg. And the same applies to taxonomies. So those are my arguments, and then finally, I register the post type. And register_post_type has two parameters. Firstly, the name of my post type, and you can see, that's tutsplus_city. So I don't want that to be the slug for my individual posts on my archive pages, which is why I've used rewrite city up here. And then my second argument, my second parameter is those arguments, so that will register my post type. So if I save that, and go back to my site, I'll refresh my dashboard, and there's an error on line 32. Very difficult to avoid not getting a semicolon wrong somewhere when you're typing PHP manually. So let's go and have a look at line 32. In fact, the error isn't actually here, it's here. I failed to type function, refresh, and it's much happier. So you can see here, we've now got a post type called cities. And if I click on that, I can add some new cities to my site. So that's how you register a post type in WordPress. In the next part of the course, I'll show you how to set up a template file for your post type, so that you can display it in the front end of your site. See you next time and thanks for watching.

Back to the top