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

5.2 Create a Widget Plugin: Run a Query

In this lesson, you'll continue creating the widget plugin to practice the skills you've learned in the course so far. You'll finish off the plugin by running a query and outputting its results in a widget.

Related Links

5.2 Create a Widget Plugin: Run a Query

Hello, and welcome back to this Tuts Plus course on plugin development for WordPress. In this part of the course, we're going to continue creating our widget. So we now have our custom post type registered as well as a custom taxonomy. Then the next step is to run a query. And I will create a function that outputs that query. And then I'll be able to run that function inside my widget. Now I could add all the code for the query inside my widget. But I like to put it in a function first. So that, well, for two reasons. One, is that I can use it somewhere else if I want to. And the other is it just makes coding my widget a little bit simpler and cleaner. So let's go to the code. So this time, instead of copying the query from an earlier part of the course. We did do queries earlier on and we used the WP Query, class, so you've already been introduced to that, but this query will be quite different. It's not gonna include categories, it's gonna output a list instead of articles. So I'm gonna code it from scratch because that's easier. So I'm going to call it I'm going to give it a function name. And I'm going to call it tutsplus_list_moons. First thing I'll do is set up a query arguments. So I've got two arguments. One is the post type. Which is tutsplus_moon, and the second is post_per_page. And I'm gonna output five. So those are the arguments for my query. It's gonna output the most recent five posts in the Tuts Plus moon post type. So now let's run the query. So I'm outputting my query, I'm checking if the query has got posts. So now let's output some actual HTML. So I've closed PHP and I'm opening an unordered list. Once I've opened that list, I then start to actually run the query. Because first I've checked if the query has posts and now I'm going to run it. So now that's the second part about putting a query. So I've got if query have posts, and then while query have post the post, so we'll start running the query. Making sure I type least ID not list is. So I've got a list with an ID of the post ID, and using the post class template tag to output classes for that post. So I've got a link to the permalink and then inside that link, I'll have the title of the post. And then I'm gonna close my link. And then I'm gonna close my list item. And then I need to end the query. And then finally, close my own ordered list. So I'll open up PHP here, so that these braces can be read. And then I'm going to put wp_reset_post data. I've got to reset the query. So here is my query. So I've got my query arguments, which are the post typing tuts plus moon. I've got the number of posts per page, which is the number of posts it will output in the widget. And then I've got my query, the outputs unordered list, and then within that for each post that's retrieved. It outputs a list item with a link and the name of that particular moon. So that's how I create that query. But that query isn't going to appear anywhere until I actually add it into my widget. So now let's create the next part and the final part of the plug-in, and that's the widget. Now for this, I'm gonna take the code we used in the earlier part of the course where we looked at widgets, and I'm going to edit it. We're gonna change its name, to tutsplus_Moon_Widget, we're gonna give it a new class name. And we're gonna give it a new description. I'm gonna change its name here to tutsplus_moon_widget, and the name that it's given in the widget screen to Moons List. So we now have the widget form. And all we need for this is the title. We don't need text and a link because we haven't got those. So that paragraph there for the title will stay and these two can be removed. We then have the function to update the data. And again, the text and link can be taken out and title will stay. And then we have the function to display the widget in the site. So again, we don't need link or text here. And then for the div, we will call it moons, moons-list. And then if the title is empty, we echo out that title, but I'm going to add in an h4 because that's what my theme uses. So if the title field is not empty, I'm echoing out what comes before the title, h4 class equals widget title. And then the title itself, the closing h4, and then after_title, and if that isn't the case, I want to echo out a default title. So I'm gonna copy this. Just put moons as the backup title in case the user doesn't actually add a title in the widget setup. I remove this part here with the link and so forth, because those aren't in this plugin. But what I do need to do now is I need to run my query, and to do that, all I have to do is run this function. And then my div is being closed. The after widget arguments are being echoed out. And that's the end of my widget. Now because I've done a bit of editing here, I just want to check all the braces. Yeah, that's good. So all the braces are in the right place. And then finally, we are registering a moons widget. And here we need to make sure we change this to the name of this class that is up here. And actually, in order to be really accurate, I'm going to copy and paste it. And I'm glad I did because I initially typed in moons. But it's actually moon. I'm gonna change it to moons actually. [LAUGH] I think it's not a moon widget. It's a list of moons. So there we have the code for our widget. So we've got our plugin in full now. We've got registering the post type and the taxonomy. Which we did in the last part of the course, we've got running a query to output a list of the most recent moons, most recently added to the site. And we've got our widget which runs that function and outputs the query. So if I go into the widget screen, we'll see I have a moons list widget. I'm gonna add that to my sidebar. I'm gonna save that title. And then refresh the screen on my front page. And there we have it, recently added moons. So it uses an h4, so it's consistent with other widgets in my theme. And it's got a list of all the moons. So if I click to those, I get to see any of the moons that I've added. So that's how you create a custom plugin, pulling together a lot of the techniques that we've used in this course. So we registered a post type and the taxonomy. We then output a query. And then we pulled that query into a widget. And that gives you an example of how you can use multiple techniques in plugins to create one more complex plugin. In the next part of the course, we'll wrap up and go through what's been covered in the entire course. See you next time and thanks for watching.

Back to the top