Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Enabling configuration of widgets

From: WordPress: Creating Custom Widgets and Plugins with PHP

Video: Enabling configuration of widgets

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.

Enabling configuration of widgets

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.

Show transcript

This video is part of

Image for WordPress: Creating Custom Widgets and Plugins with PHP
 
Expand all | Collapse all
  1. 1m 49s
    1. Welcome
      1m 16s
    2. Using the exercise files
      33s
  2. 23m 29s
    1. WordPress overview
      2m 32s
    2. Installing WPI for Windows
      3m 42s
    3. Installing MAMP for the Mac
      3m 25s
    4. Installing and configuring WordPress
      5m 51s
    5. Comparing WordPress 3.0 with previous versions
      2m 57s
    6. Setting up a PHP/WordPress development environment
      5m 2s
  3. 14m 47s
    1. Exploring WordPress plugins
      3m 42s
    2. Administering plugins from the WordPress admin
      5m 23s
    3. Exploring where plugins reside
      2m 51s
    4. Introduction to hooks
      2m 51s
  4. 39m 28s
    1. Creating the plugin PHP file(s)
      3m 12s
    2. More on hooks: Actions and filters
      3m 15s
    3. Installation and activation
      4m 6s
    4. Writing activation code
      3m 45s
    5. Writing an action
      5m 12s
    6. Writing a filter
      4m 15s
    7. About pluggable functions
      2m 1s
    8. Writing a pluggable function
      5m 30s
    9. Using template tags
      2m 46s
    10. Introducing shortcode
      5m 26s
  5. 26m 2s
    1. Widgets and the WordPress Widgets SubPanel
      2m 54s
    2. Comparing widgets and plugins
      1m 8s
    3. Using and customizing built-in widgets
      3m 18s
    4. Creating a new widget
      7m 21s
    5. Writing the constructor and registering widgets
      5m 20s
    6. Enabling configuration of widgets
      6m 1s
  6. 44m 59s
    1. Creating an admin interface
      5m 25s
    2. Saving data to the database
      5m 39s
    3. Securing form submission with nonces
      2m 25s
    4. Options editing post-WordPress 2.7
      4m 8s
    5. Integrating with the WordPress admin menus
      3m 34s
    6. WordPress admin dashboard API
      4m 5s
    7. Using existing options and option editing pages in WordPress
      5m 19s
    8. Using jQuery and AJAX for administration
      14m 24s
  7. 27m 13s
    1. Accessing the WordPress database
      5m 45s
    2. Using the built-in schema
      2m 21s
    3. Accessing data using $wpdb
      5m 15s
    4. Creating new tables
      7m 18s
    5. Inserting data
      6m 34s
  8. 26m 27s
    1. Introducing the Loop
      6m 22s
    2. Using WP_Query()
      3m 11s
    3. Custom filtering and sticky posts
      4m 58s
    4. Using jQuery and AJAX for posts and pages
      11m 56s
  9. 12m 9s
    1. Registering and promoting plugins
      2m 28s
    2. Creating an uninstall function
      5m 53s
    3. Backward compatibility issues
      3m 48s
  10. 15m 3s
    1. Understanding security issues
      11m 20s
    2. Internationalizing your plugin
      3m 43s
  11. 18s
    1. Goodbye
      18s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

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.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Join now "Already a member? Log in

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed WordPress: Creating Custom Widgets and Plugins with PHP.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

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

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

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.

Are you sure you want to delete this note?

No

Your file was successfully uploaded.

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:

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:

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

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.