Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m
by
FREELessons:15Length:1.6 hours

Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

4.2 Content Creation Plugins

Hello, and welcome back to this Tuts+ course on WordPress plugin development. In this part of the course, I'm gonna show you how you can use a plugin to create custom content types. And that includes custom post types, custom taxonomies, and custom fields or post metadata. So a custom post type is a bit like a post or a page. Which are the default post types that come with WordPress. You might use it to add something like products, if you're running a store, events, if you're running some sort of events type. Events, if your site is advertising or selling tickets for events or anything that you want to list. So, for example, on my writing site, I've got books as a post type. A taxonomy is something slightly different. There are built-in taxonomies in WordPress. And the ones that you're most familiar with are categories and tags. But you can add your own taxonomies. And you'll often find that if you're writing a post type, you'll want to add a custom taxonomy in order to classify that post type. And then finally, a custom field is additional metadata that you add to an individual post that tells users, tells people visiting your site, a bit more about that post and information about it. So, for example, on a site that I manage for a client that includes vacancies, which is a custom post type, that has a taxonomy which is the kind of vacancy. So if it's managerial or administrative, for example. And then it also has custom fields for things like the salary, the closing date and the interview date. So these codex pages will give you more information about those custom content types. And once again, I'm gonna walk you through some courses that have already been created and the code that was created for those. So here we've got a code called Using Custom Content Types in WordPress. So here we've got a course called Using Custom Content Types in WordPress. And here's another course, which is just a short one, called Registering Custom Post Types and Taxonomies in WordPress. So this first one is a longer course, and it goes into more depth on custom post types, taxonomies, and also custom fields and post metadata. And custom fields and post metadata are the same thing. They're just different words for the same thing. So let's take a look at my site. So here I've activated one of those plugins and I've registered a custom post type called Moons. And so I can add a new moon, not necessarily the new moon. And I haven't got any added here, but I could add new moons if I wanted to. And these also have a custom taxonomy, which is Planets. So I could add, for each moon, the planet that it orbits. And that's an example of a custom post type that could be registered. So let's take a look at the code. So here's my plugin to register my post type and my taxonomy. So I'm creating a function here called tutsplus_register_post_types. Although I'm actually only registering one post type here, but I could register more than one in this function. I'm giving it an array of labels and also an array of arguments, which includes the labels. And then I use the register_post_type function, which is provided by WordPress, with two parameters. The first one being moon, and the second one being arguments. And then I hook that. And here you can see my function name here, tutsplus_register_post_types. I hook this to the init hook. And then again, to register a taxonomy, I've created a function called tutsplus_register_taxonomies. I've got an array of labels for it and an array of arguments as well, which includes those labels. And these labels, in both cases, are the text that you'll see in the admin screens when you're editing your WordPress site. And you can see that I've internationalized the code by using internationalization functions here. And in this case, I used the register_taxonomy function. And that has three parameters, the name of the taxonomy. And to be honest, I should probably call it tutsplus_planet to make it unique. Because you never know, there might be another plugin or there might be a theme running on my site that has a taxonomy called planet. It's unlikely, but you never know. And then you can use rewrite to change the slug so that it doesn't show a slug of tutsplus_planet. So the taxonomy here is planet. Moon, the second parameter, is the post type that it applies to. And that can be an array, you can apply it to more than one post type. And then finally, all of my arguments. And then again, Using add_action, which we've looked at earlier. So that's how I register a custom post type and a custom taxonomy. But what about custom fields or post metadata? Now this course here shows you how to display your custom fields in a WordPress theme. Now in this particular course, I've used a theme, I've used the theme template files to display those. But you could use a plugin that hooks into any hooks that are in your theme if you're using a third-party theme that's got hooks, or if you're using a plugin like WooCommerce that's got lots of hooks. So let's take a quick look at the code for that. So here we've got this theme tutsplus-display-custom-fields-in-posts. And I've got my content-single here. And I've added an aside here for the post-meta. So I've added a particular custom field which I'm outputting here. So the key function I'm using here is get_post_meta, and that's got three parameters. The ID of the post, the name of the field, which here is Day of the Week, and whether I just want to output one or whether I want to output all of them. And here I've put true because I just want to output one. So I've used that twice for the day that I've written, the day of the week and also my mood on the day of writing that post. And that will be output at the bottom of every page. And there are lots of other ways you use get_post_meta. So if you create meta boxes, as we saw in the previous module on customizing the admin, you can give your users somewhere to add metadata for an individual post via your plugin. And then you could output that metadata somewhere on the post or elsewhere on the site, depending on what you need. So that's how you work with content types in your plugins. In the next part of the course, I'll show you how plugins can be used to output either static content or the results of a query. See you next time, and thanks for watching.

Back to the top