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

3.2 Registering a Taxonomy

In this lesson, you'll learn how to register a custom taxonomy by writing a plugin.

Related Links

3.2 Registering a Taxonomy

Hello, and welcome back to this tuts plus course on custom content types in WordPress. In this part of the course, we're going to register a taxonomy to go with our cities post type. So just for this city's post type, there will be a taxonomy called country. So let's take a look at code. So once again, I've created a plugin for this, and this is called tuts_plus_register_taxonomy. I've given this a version number which is the number of the part of the course we're on. And I've added the other information that WordPress needs for a plugin. So now, I need to type in my function. Remembering this time to type the word function. So this is also hooked to the init hook. So I'll copy the name in my function here. So that's my add_action function with init as the first parameter and the name of my function as the second parameter. And once again, we start off by creating some labels. [SOUND] Labels, not labelles. [SOUND] And again, it's an array. So we'll start with the name. So that's my name, and then I need to add a singular name. Which will be country and then I need to go through and add some more labels. And I'm doing this a little bit out of order, because I'm trying to put them into some sort of logical order. I'm working from some notes that don't have them in a logical order here. And I'm trying to put them in a logical order. But to be honest, it makes no difference whatsoever what order you put them in as long as you put in the ones that you need. So those are all my labels. Now, I need my arguments. And it's very similar to registering a post type. So my labels are, the labels variable, I've already defined and then I'll be adding other arguments, some of which is similar to post types and some of which are different. So hierarchical. And I like to use hierarchical because that means that you can see the different options for your taxonomy in the user interface. A bit like you would with categories but now with tax. So hierarchical taxonomy will behave like categories but not like tax, which are not hierarchical. So I've got sorting and the arguments by which they're sorted. I've got rewrite, Which I'm using again, because of the fact that I'm using a prefix for the name of my taxonomy, which isn't very pretty. So the rewrite enables me to have pretty slugs. Show_admin_-column means there will be a column for this taxonomy in the overall admin screen that lists all my posts of the relevant post types. Something that's not there by default, and you need to add and then finally show_in_rest. Which means I can access this via the screens if I've got a version of WordPress with Guttenberg. And it can also be accessed by the REST API. So those are my arguments and then I use the register taxonomy function, To actually register my taxonomy. So the first parameter here is the name of the function. Now, I like to use prefix here so that I know that my taxonomy sorry, the name of the taxonomy, not the name of the function, so that I know that name and my taxonomy will be unique. Because I don't want to clash with any other plugins or themes I might have installed. And then my second parameter is those arguments. So that's my function written. Now let's check the site. So if I refresh my site, now, again I'm getting an error message which is rather odd because it's referring to media.php. So let's take a look at the file. Right, well I know I've done one thing wrong which may or may not be related to that error message. And that's that the register taxonomy function here, has three parameters because the second one is the post type that the taxonomy relates to. Now that can be just one post type or you can use an array to register it from multiple post types. So I'm going to change that to tuts plus city so that it will be registered for my post type that I already created. Now let's go back to my site and refresh and that error message is gone. So not quite sure how that was related, but it clearly was, and then if I look at cities, I've got country here as well. So, I can click on that and I can add new countries, and then I can apply those to my cities. So that's how you register a custom taxonomy in WordPress. In the next part of the course, we'll look at taxonomy terms. And I'll explain how those work and how to create them. See you next time and thanks for watching.

Back to the top