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

Next lesson playing in 5 seconds

  • Overview
  • Transcript

5.1 Create a Widget Plugin: Register the Post Type and Taxonomy

In this lesson, you'll combine the skills you learned to this point in the course in a practical project: a widget plugin. You'll start by registering a post type and taxonomy.

Related Links

5.1 Create a Widget Plugin: Register the Post Type and Taxonomy

Hello, and welcome back to this Tuts+ course on plugin development. In this part of the course, we're going to combine a lot of what you've learned so far into a practical project. And in this project, I'm going to create a widget, but this widget will also incorporate some of the other techniques we've looked at. So what it'll do is, it'll take a custom post type, it'll query that custom post type, and it'll output the latest posts of that post type in a widget. So it'll use queries which we've looked at, it'll also use custom post types, which we've looked at, and it'll use widgets, which again, we've looked at. And this will combine them all into one plugin, so let's start with the code. So here I've got my empty plugin file, I've added some comments so I know what I need to do, and I've also added the commented out text at the top here. Which will tell WordPress that this is a plugin and what it does. So let's start by registering the post type, now we covered registering post types in an earlier part of the course. So, I'm gonna go back to that code that we created there, and I'm going to copy it and edit it. Because once you've written some code once, it's much more efficient and also more reliable to copy it and use it again with edits. So let's copy that and add it into our moons widget, because this is going to be a moon post type. So now we have our labels for our moon post type, and here we've got a rewrite, which is moons and we're also going to change the name of the post type to tutsplus_moon. And I'm gonna change this to tutsplus_register moon post type my function name, just so I know it's unique because we've been setting up a lot of plugins on this demo site and I don't want to accidentally have to, that are clashing with each other. So, there is our post type being registered, so it's got labels with moons in, it's got these arguments, including those labels, the fact that it has an archive that it's public, that it's not hierarchical so it will behave like a post rather than a page. That it's got this slug of moons rather than tutsplus_moon as its slug when we're looking at it in the browser, and that it supports the title the editor, the excerpt Custom-fields, thumbnails of featured images and page-attributes. And it will also apply to the post-tag and category taxonomies so we use tags and categories with it. But will also have a taxonomy that is specially for the moon post-type, and that's what I'll add next. Let's just tidy things up a little bit here, and I'll add one more gap, so now I'm going to register the taxonomies, so here is the code where I registered a taxonomy. So you can see actually, I already registered a moons post type, so I should have copied that, shouldn't I? But that just shows how you can take existing code, that you've used and edited to make it apply to a new post type. So here, we've got labels, we've got a singular name for it, and I'm keeping this taxonomy the same planets and got all of these labels for it, the rewrite slug is planets. And this time I'm gonna call it tutsplus_planet, tut's, okay so know where that came from and have, Tutsplus_moon as the second argument, so that's my function to register my taxonomy, I'm going to slightly change the name of it, to be sure that it's unique, And I also need to add another argument. Which is showing rest, and the reason for that is because of the Guttenberg interface. So Guttenberg uses the REST API to access posts, and taxonomies and everything else from the database. And without access to this taxonomy, it won't show up in the post editing screen for any post types that the taxonomy is registered for. So you need to include this show_in_rest, to make sure you can still access it If you're using the Guttenberg post editor, which you will be if you're using an up to date version of WordPress and you haven't installed a plugin to turn it off. So the first two parts of my plugin are complete, I have my post type, and I have my taxonomy. And if we take a look at the site, you can see that these are added to the site, so that's active because I've activated the plug in. So I've got planets as a taxonomy, I've added some moons, I haven't actually added the planets to them yet, but I could do that if I wanted to, the planets won't be called by the widget plugin. We could add those in if we wanted to, but for the sake of this particular plugin, it's just gonna output a list of the most recent moons that have been added to the site. So in the next part of the course, we'll move on to start creating that widget, so we'll create a query that will query for the most recent moons, and we'll create a widget that runs that query, see you next time, and thanks for watching.

Back to the top