Building a WordPress widget is pretty much the process of building a plugin, but we use some special techniques to do it. Everything is going to happen in a single file using the WordPress Widget API. As long as you know the basic framework, everything will fall right into place. You need to code your widget using three major functions: Widget, Update, and Form. In this unit, we are going to discuss generating the widget interface using widget and registering the widget. So let's to into PDT eclipse, and you can see I have created a new file called simpleWidget, and I have put all my comments in it, just like I would have with any other plugin.
Since WordPress 2.8, they added an object- oriented methodology for creating widgets. There is another way to do it, and you can see it online in the main Widgets page in the Codex. There's a special section about creating widgets before 2.8, and this is a fine way to do it; however, creating it the object- oriented way is a lot cleaner and neater, and is considered the best practice. So the way to do this is we actually create a class file. So creating classes in PHP is a matter of declaring the name of your class, giving it a name--in this case, we will call it SimpleWidget--and if it's going to extend any specific class, that is, if it's going to inherent things from another class, then you need to define it by saying extends, and then in this case, there's a special class called WP_Widget, which is made specifically for creating widgets.
So this is a basic class definition, and you can write other plugins using classes as well. However, in this case we are just going to do it for the widget. Every class has a basic function called the constructor, and this is a class that's called when you create a new instance of this class. So we are going to declare ours as SimpleWidget. Notice that it matches exactly the name of the class. Then we need to declare three special functions, and these are what make widgets work. The first function is widget.
The widget function is going to output the user interface. We also have an update function, which will be used for handling any update functionality, and finally a form function, which will be used if we have any configuration options. Once you have all these declared, the next main step is going to be to create the actual user interface using the widget function. So this function is going to take two arguments.
The first one is called args, which are basically going to be the arguments from the theme. So this function is going to get called internally whenever a widget is pulled into the user interface, and when it calls it, it's going to pass to it the arguments from the theme which allow us to get some specific information from that theme, like all of the HTML that comes before the widget, all HTML that comes after it, the title and some other information. So we declare that, and then we declare an instance.
An instance is going to give us essentially an instance of the class. In here, the first thing we want to do, to make our lives easier we can use a special function called extract. Extract will take whatever you pass it as an object, it will parse it out, and it will then put it into variables that you can use in your function so that we don't always have to use the prefix. So we are going to essentially take all the args out and make them local variables, and we are going to use this EXTR_SKIP constant.
We are going to pass to it as well. That will essentially protect any existing variables. Now we can set our title equal to, and this is a funny little bit of code. So we are looking at the instance property, and we're going to see if there's a title in here. So notice how it's wrapped in parentheses. Basically what we are doing here, this is a shorthand if-else statement. So we are saying if there is something in this instance title, then we are going to set our local title variable equal to that value.
Else we are going to set it to "A simple widget." So this is essentially setting a default title. In addition, we are going to create a body and do the same basic process. Look to the instance and see if there's a body property, and if there is, then we will set the body equal to that property, and if not, the body will just some simple text.
On your example you might want to say, let's say your widget is outputting some kind of list of e-mails or something, you might want to have a default message that says, "No e-mails available," because if there's no body then that means there's obvious nothing to output. So now what we are going to do is since I am going to be writing some HTML--because this widget function is actually outputting user interface--I am going to close my PHP, and that then allows me to just write as if I'm in a regular PHP page. So I am going to echo this special variable, before_widget, and this came from our arguments, and I can access it like a local variable because I used this extract function.
Then I am going to output before the title, and I am going to append to it the title and append to it after title. So that's going to essentially output the title and then wrap it in this before_title and after_title, which is usually some form of div tag or some kind of HTML. Then I am going to write a paragraph, and inside the paragraph, I am going to echo whatever is in the body, which is going to be, of course, whatever was passed into this instance of the widget, or it's going to just put the default if nothing was set, and then once I am done, make sure to reopen the PHP, so that I can continue to parse the rest of the page.
So this is the basics of putting together a widget using WordPress's Widget API. We will talk about how to register this widget and use it in a WordPress environment later. I'll note that widgets are meant to go hand-in-hand with themes. If you want to know more information on developing themes and using themes, please refer to the lynda.com course on that topic. So overall building a widget using Widget API requires a little understanding of object-oriented programming. Because we are extending this built-in WP_Widget class, we have these functions available to us that ensure everything is created in a consistent manner.
In this unit, we use the widget method to output the widget body itself. In the next unit units, we are going to talk about how to create update and how to create forms to allow users to edit some configuration options and save those to the database.
Get unlimited access to all courses for just $25/month.Become a member