Video: Writing the constructor and registering widgets

In order for a widget to be used in the WordPress environment, we need to first register it. This is going to be done using a WordPress action called widgets init. Additionally, if we want to set up any configuration elements, we need to do this in the widget itself. We are going to do it in the constructor. So let's go back to our class. You can see we have declared our SimpleWidget class that extends the WP_Widget class. We created an empty constructor, and we've also created the functions, widget, update, and form. So now to register it, we are going to create a function that's going to do the registering.

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.

Writing the constructor and registering widgets

In order for a widget to be used in the WordPress environment, we need to first register it.This is going to be done using a WordPress action called widgets init.Additionally, if we want to set up any configuration elements, we need to dothis in the widget itself.We are going to do it in the constructor.So let's go back to our class.You can see we have declared our SimpleWidget class that extends the WP_Widget class.We created an empty constructor, and we've also created the functions,widget, update, and form.So now to register it,we are going to create a function that's going to do the registering.

We want to make sure to do this outside of the class, so that it getsexecuted when we activate.So we will declare function.We will call it simple_widget_init, and in here, we will just call the specialregister_widget class, and we pass to it the name of the class.So make sure that that simple widget matches the class that you have up here,because that class definition is going to set up when your class gets run.

Then we need to add the action for widgets_init.We want to call the simple_widget_init.So this should set up our widget and make it so that we can use it in theWordPress environment.So let's go back to the administrator.If we scroll down, we will see our Simple Widget is listed here, but it's not activated yet.So let's go ahead and activate it, which will run the code, which will call thatfunction once the widgets_init method gets run.

So let's go into the widgets, and we should see our widget registered.Now you wouldn't know it, but it is here.It's this one. Because we didn't set up any of the configurable elements, some ofwhich will actually display here,it doesn't say anything, and we will look at doing that in a second.But let's go ahead and just grab it and drag it over here, and you can see thatit will actually work.So if we go back to the front-end of our web site and refresh it, you can seeit appears down below, with the default information that we had displayed.

If you want to display some of those options, which you most likely do, we aregoing to do that by setting it up in the constructor.Specifically, what we are going to do is we're going to call the parentconstructor of the WP_Widget class.You can find information for the WordPress classes online atphpdoc.wordpress.org.This is the definition for the WP_Widget class, and you can see it will tell youhow you use the class, and it also tell you the inheritance chain, but inaddition, it will tell you how the constructor works, you can see theconstructor is going to take the ID, a name, and then some options in anassociative array, both widget options and control options.

The widget options are going to have information metadata about the class, andthe control options will have size information.So let's first create a variable to hold our widget options.We are going to set this equal to an associative array, and we are going to setup a classname and set that equal to simple-widget.We are then going to set up a description, and we will call this "Just a simple widget."So there is our metadata.

Now we need to pass this into the parent class by calling the super constructor.The way you access the parent class is you use a special parent keyword,then these two colons, and then you can determine which method you want to run.In this case, we're going to run the constructor, so it will look like we arecalling a method with the same name as the class. And we pass in the ID--you want to make sure that this is unique--the title--this is what's going to get displayed on the bar of your widget whenyou're in the back-end--and then any of our options. In this case, we have just a widget options.

You could, additionally, pass the configuration option with the size information.Make sure that this variable here matches this.You can see I made a mistake.It should have an S there.So let's go back to the administrator, let's remove our widget, and let's goahead and refresh the widget page, so our simple widget will appear.It's not in the top any longer because they're in alphabetical order.So now you see it's down here after the RSS, and after the Search.Simple Widget, which was the title, and Just a simple widget came from the description.

It will still behave the same, and I don't have any configuration options.We will talk about those later.So that's the basics of adding a custom widget to the WordPress installation.Basically we do it like we do a lot of other plugins:We add an action, and that's specifically is the widgets in that action.We are going to then, inside that action function, we are going to call theregister widget method,which is going to set up our widget for use.Finally, if we want to do any configuration options, we can do those by callingthe super constructor inside of the constructor class.

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 "" :

Expand all | Collapse all

please wait ...

Q: Do I need a web hosting service for this course?

A: You don't need a hosting site to do any testing or development work that’s covered in this course. However, if you want to have your WordPress site available to the public, you will most definitely need a WordPress site. If you are hosting with an independent company, they will need to have PHP and MySQL installed, and there will be some configuration differences, but basically, you can upload anything on your local version to the web site. If you are hosting with Wordpress.com, you will need to add your plugins by uploading them manually through the WP Admin Plugin screen.

Sorry, there are no matches for your search "" —to search again, type in another word or phrase and click search.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.

Already a member ?

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships.
Learn more

Upgrade to our Annual Premium Membership today and get even more value from your lynda.com subscription:

“In a way, I feel like you are rooting for me. Like you are really invested in my experience, and want me to get as much out of these courses as possible this is the best place to start on your journey to learning new material.”— Nadine H.

Thanks for signing up.

We’ll send you a confirmation email shortly.

Sign up and receive emails about lynda.com and our online training library:

new course releases

newsletter

general communications

special notices

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

new course releases

newsletter

general communications

special notices

Here’s our privacy policy with more details about how we handle your information.