Start learning with our library of video tutorials taught by experts. Get started
Viewed by members. in countries. members currently watching.
In WordPress: Creating Custom Widgets and Plugins with PHP, Drew Falkman teaches PHP developers how to create custom functionality for WordPress 2.0 through 3.0 using widgets and plugins. This course starts by installing and setting up WordPress 3.0 on both Mac and Windows, then provides an in-depth look at tasks related to these WordPress add-ons: installing and administering, building and customizing, creating editable options and database tables, working with posts and pages, and utilizing jQuery and AJAX. There are also tutorials dedicated to promoting a widget or plugin, adding security, and localizing the interface. Exercise files are included with the course.
In many widgets, it will be crucial to have some kind of settings that enable the administrator to set configuration options of the widget. A weather widget for example is going to have a setting to allow you to enter the zip code to know what weather to display. The WordPress Widget API gives us a couple key functions in the WP_Widget class to do this: the form function and the update function. Let's take a look at how those work. So here's our simple widget that we created that extends the widget class. We have already created the setup options and registered the widget with the administrator.
So let's go ahead and go into our form and first things first: let's go ahead and close the PHP. This will essentially allow us to type anything after here as if we are typing in a PHP file, because it's going to output HTML. Just so we don't forget later, let's go ahead and add that PHP opening tag again, so that it continues to parse the rest of the document normally. We don't have to worry about the actual Form tag itself; that will be already handled by the Widget API.
We do need to write whatever fields we want to be editable. So I am going to create a label, and this is going to be a label for--and then I want to output the ID for the Title field because we're going to allow the user to edit the title and the body. So to output that, I use echo, and then I use this which refers to the instance of this class, and I use get_field_ID, a built-in method that allows me to get just these types of data, and then we'll close our for and close the label tag.
Then we'll input our Input field, and we are going to get the ID in exactly the same way. In fact, you can literally just copy it and paste it. We'd also need to get the Name field, and we do that in a similar way, although we actually use the get_field_name method, and it's for the same field. The last thing is to output the value, and we are going to get that directly from the instance.
So that's actually going to get the value that's saved in the instance. And to get the instance, we can pass it to the form. In fact, this is going to get passed to the form whenever it's called internally by the Widget API. So in here we're going to use php echo, and then we will use instance and access it by its name, title. A good idea, whenever you're accessing strings that the user entered into HTML of some kind, is to actually escape the HTML, and we can use that using the esc_attr function.
So we will just wrap that around the variable. So I am going to do the same basic thing for the body tag, so I am just going to go head and copy this and paste it in, but I am going to change this label to body. I will change this to read body, and instead of using an input, I am actually going to use a textarea. The textarea is going to have an ID of get_field_ID('body') and the field name is going to be get_field_name('body').
However, the difference between text area and input is that we don't use the value property. We instead put the value in between the open and closing text area tags. So in here, we will output echo esc_attr, instance, body. So there we have it. I think we're all ready to go. Let's have one last final look, and make sure it's all clean. So let's save it and go back to our administrator, and let's refresh the Widget page.
We can now grab our widget and drop it over here. So you can see I now have my Form that I input here. I have my title, and I can say "My Message:" "Hello." But when I save it, nothing happens. The reason is that it's calling the update method to save this, and our update method is empty. The beauty of inheritance in object oriented-programming is that we inherit the built-in functionality of the original class.
In this case, we're overwriting it because that is exactly what customizes it. So we create our widget. We create our form because we want to create forms and widgets that are specific to this widget. However, update, we just want to save this data, and we can actually use the built-in update method to do that. So I am actually going to delete the update method from my class. I am going to go back into my Widget class and re-enter my data. So I will put "Today's Message," "Sunny in Ventura, CA." Since it called Today's Message, it then save this information and if I were to open this in my browser, you could see that it output "Today's Message, Sunny in Ventura, CA." So it saved it for me.
If you wanted to, you could actually create an update function. It's going to take two arguments: it's going to take the old instance and the new instance. And you can then do some validation and then use data from either one. It's a good way if you want to do validation or some kind of data scrubbing in between. But if you just want to use it out of the box, there's no need to use it. So the WordPress Widget API allows developers to add configuration options that will be stored in the instance array. This is done through the form function, which essentially is going to output the user interface in the form, which you can use get_field_ID and get_field_name of this to get your data.
And finally, you are going to use the update function to save your data. If you want to overwrite the update function, you can do that as well, to perform validation in such.
Find answers to the most frequently asked questions about WordPress: Creating Custom Widgets and Plugins with PHP.
Here are the FAQs that matched your search "":
Sorry, there are no matches for your search ""—to search again, type in another word or phrase and click search.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.