Unlimited WordPress themes, graphics, videos & courses! Unlimited asset downloads! From $16.50/m
by
Lessons:7Length:39 minutes

Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

2.1 Get Started Registering a Custom Post Type

In this lesson, you’ll learn where you should register the post type and get started writing the code.

Related Links

2.1 Get Started Registering a Custom Post Type

Hello and welcome back to this TUTS+ course on how to use custom post types in WordPress. In this part of the course, we'll start to register our post type. And we'll create the code that you use to do just that. But before we start that, let me just take a moment to consider whether we should add that code in a plugin or in the theme. Now, it may be tempting to think that because you're displaying your post types, along with other content from your site, that it makes sense to put it in your theme. But the problem with adding the code for your post type, for example, for the functions file in your theme, or to an include file that you could call via your functions file. Is that if you switch themes in the future, you'll lose your post type and you won't be able to access all of the data that you've created for that post type. So it's better practice to use a plugin and that's what we're gonna do here. At the moment, you can see that there isn't a plugin on our site for registering custom post types. Now, I've actually created the file for the plugin, but because I haven't added any code to it yet, WordPress doesn't recognize it as a plugin. So let me show you the file. So here is my empty plugin file, I'm working on a local site so you can see here it's in my local dev folder, and it's in plugins within WP content. Now, to create a new plugin you can either create a folder for all your plugin files or you can just create a file. Because all I'm doing is registering a post type with this plugin, I've just created a file. So I'll start off by adding some commented out text at the top that tells WordPress that this is a plugin. So there's our comments that tell WordPress that this is a plugin. So first, we have the name of the plugin. Then we have the URI where people can find out more about it. The description, Which I'll correct. And the version number, the author, and the author URI. So I'm gonna save that and then go back to the plugin screen in my site. So there's my plugin to register the post type displayed on my Plugins page. I'm going to activate that, but once I've done that, it won't actually do anything. I haven't got post type appearing over here, and that's because I still need to add the code to do that. So let's get started with that stage. To register my post type, I need to create a function which I then hook to the init hook. And within that function, I'll use the register post type function. But before I do that, I need to set up the labels and the arguments for my post type. Now, if you want to know more about register post type, you can see it on the codex here. And also, there's this guide to custom post types on Tuts+. But let's go back and start on the code. So I've started off by adding the name of the function and what it does in comments. And I like to do that before all my functions, so that I can easily find them within my files. Because if I add more code to this plugin file at a later time, I want to be able to find everything easier. So let's start by writing the function. I'm gonna copy this here. And those braces will contain my function. And I'll need to hook the function to the init hook, and I do that by using add_action. So add_action has two parameters. The first one is the hook, the action hook that I'm hooking my function to. And the second is the name of the function, which is tutsplus_register_post_type. Now, you'll notice that I've used a prefix here so that it doesn't clash with any registered post type functions in any other plugins that I might have running on my site. And you should always use a prefix for good practice. So now, let's start writing the code to create our post type. And the first thing I'm gonna do is create some labels. Now, labels go in an array because there are a number of labels for your post type. And those will dictate what shows up in the WordPress admin when somebody is working with that post type. And these are just for the admin screens not for the frontend of the site. So there's the first line in my array and that is the name of my post type. That isn't its actual name when it comes to how WordPress works with it, but that is the name that somebody will see in the menus. And you always put that in the plural and then we'll follow that with a singular name, as well as some other labels. So I'll type all of those in and then I'll work through them with you. So those are our labels. And you can see that as well as the name, there's a singular name, and a label for adding a new project, and a new item. And these will appear in slightly different places in the admin for editing, for the new project without adding, for viewing, searching, and the text that's returned if none are found when you're searching. So these aren't essential. If you don't put these in, what WordPress will do is it will just substitute post for this. And it'll give you the same labels as you would use for the post post type. I like to add these labels because otherwise I confide it gets a bit confusing if it starts telling you that your projects are actually called posts. And particularly, not necessarily for me who's familiar with the code and the post type that I've registered. If somebody else was to come to use my site or to use my plugin and work with that post type, I want to make sure that they have labels that made things as clear as possible. So those are the labels. The next thing we do is create some arguments. And again, those are in an array. And I'm gonna show you the arguments in more detail in the next part of the course because there's quite a lot to cover there. But then finally, we then register the post type. Now, this function, register_post_type, has two parameters. The first one is the unique name of the post type. And again, I'm gonna give it a prefix so it won't clash with any post types that are being registered by plugins or by my theme. The second parameter is the arguments. Now, if you wanted to, you could within here just put this register_post_type function. And then type out the arguments in detail as I'll show you, which will include the labels. But I prefer to separate out my labels, and then my arguments, and then register the post type, not forgetting my semicolon at the end. So that's the code that we start with to register our post type. We've got our labels set up, and our register_post_type function. But it's not finished yet, because we need to add those arguments. And that's what I'll cover in the next part of the course. See you next time, and thanks for watching.

Back to the top