How to create a WordPress Widget – Part 3 of 3

Phew! We made it!

So, all we need to do is add one final little bit. Really little bit…

function widgetTutorial_init() {
  // These are the WordPress functions which will register
  // the widget, and also the widget control - or
  // 'options', to you and me.
  register_sidebar_widget('Widget Tutorial', 'widget_widgetTutorial');
  register_widget_control('Widget Tutorial', 'widgetTutorial_control');
}

add_action("plugins_loaded", "widgetTutorial_init");

That’s it. Disappointed? I hope not, because these few lines make the whole thing work. Let’s take a look at them in turn…

Initialise the widget

Here we have a simple funtion that we’ll call on the last line of the code. It really contains only two lines of php:

register_sidebar_widget('Widget Tutorial', 'widget_widgetTutorial');
register_widget_control('Widget Tutorial', 'widgetTutorial_control');

Again, we using calls to built-in WordPress functions here. The first, register_sidebar_widget(‘Widget Tutorial’, ‘widget_widgetTutorial’);, registers the widget for use on the site itself. It calls the widget ‘Widget Tutorial’ and we point it to the function we have written called ‘widget_widgetTutorial’. No more to it than that.

The second, register_widget_control(‘Widget Tutorial’, ‘widgetTutorial_control’);, is the WordPress function for the Dashboard, and tells the Widgets page where to look for our options. Again, we name the widget ‘Widget Tutorial’, and this time point towards our options function ‘widgetTutorial_control’.

You can change the name to whatever you want, and make sure it points to the right function (whatever you have called it).

And finally, after that final function, we register the widget with:

add_action("plugins_loaded", "widgetTutorial_init");

Again, pretty simple WordPress stuff here. We add an action so that when all the plugins are loaded we run the WidgetTutorial_init function – which we’ve just written.

And that, ladies and gentlemen, is that.

Next, we get more complicated and put together a Widget which displays, amongst other things, Flickr photos. But you’ll have to wait for that as this site needs a bit of an overhaul.

As ever, if you have any questions please drop me a line. You can also download the complete code and play with it.

Ranger, out.

The Complete Code

http://www.richardshepherd.com
Description: Simple Widget Tutorial from richardshepherd.com
Author: Richard Shepherd
Version: v1.0
Author URI: http://www.richardshepherd.com
*/

function widget_widgetTutorial($args) {
 // First we grab the WordPress theme args, which we
 // use to display the widget
 extract($args);

 // Now we sniff around to see if there are
 // any preset options
 $options = get_option("widget_widgetTutorial");

 // If no options have been set, we need to set them
 if (!is_array( $options )) {
  $options = array(
    'title' => 'Widget Title',
    'text' => 'Widget Text'
    );
 }

 // Display the widget!
 echo $before_widget;
 echo $before_title;
  echo $options['title'];
 echo $after_title;
  //Our Widget Content
    echo $options['text'];
 echo $after_widget;
}

function widgetTutorial_control() {

 // We need to grab any preset options
 $options = get_option("widget_widgetTutorial");

 // No options? No problem! We set them here.
 if (!is_array( $options )) {
  $options = array(
    'title' => 'Widget Title',
    'text' => 'Widget Text'
  );
 }

 // Is the user has set the options and clicked save,
 // Then we grab them using the $_POST function.
 if ($_POST['widgetTutorial-Submit']) {
  $options['title'] =
    htmlspecialchars($_POST['widgetTutorial-WidgetTitle']);
  $options['text'] =
    htmlspecialchars($_POST['widgetTutorial-WidgetText']);
  // And we also update the options in the WordPress Database
  update_option("widget_widgetTutorial", $options);
 }

?>




 




5 thoughts on “How to create a WordPress Widget – Part 3 of 3

  1. Pingback: How to create a WordPress Widget – Part 2 of 3 | Richard Shepherd

  2. Awesome tutorial. I’ve been looking all over net for one I can understand. I find WordPress’s own documentation hard to follow.

    Grate job! You’ve helped me get off the starting blocks. :)

  3. Dear all:

    Thank you very much for this great tutorial, save much time.

    I was wondering if someone knows how to update idget options dynamically (not based in user input, but dynamic detection).

    Let me put an example:
    Page 1 –> FAQ –> Show FAQ categories/subcategories. All the time I am here (seing posts, o browsing categories, etc.), the categoris widget show FAQ categories/subcateries
    Page 2 —> Shop —> Show Articles categories/subcategories.

    So, I detect page, the the category_id of the widget is updated. No user interaction here.

    Whatever I do in the widget function, options are not updated. Any idea?

    Thanks, César

  4. I want to make unique table for my widget (e.g. save url to image for home page).
    On admin page widget will show image from that table and user would be able to change this image.
    So, how script knows in which table of database it must put data about image?
    Thanks.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>