And dissected at that

If you have even a tiniest bit of ambitions, you will want to have some special information in your blog’s sidebar, be it a link to your RSS feed or links to your co-authors’ social profiles. Plugins are a nice way to get what you want, but they don’t always match your needs. For some purposes you will want to have your own widget, and creating one in WordPress higher than 2.8 is pretty damned easy, which I will demonstrate you in this not-anime-related-at-all post.
You have two places where you can put your widget’s code in: your theme’s functions.php or into a plugin. It’s pretty equal where you put it.
The Base
The whole widget, as gladly implemented in WordPress 2.8, is situated in a PHP class. Gladly because it’s much, much simpler to do basic actions like giving the user options and saving them like that. Let’s begin the class:
1 2 3 | class new_widget extends WP_Widget { //All below stuff will come here } |
These are some nice three lines of code. Let’s create a constructor function now. Basically, it means we write a function that will tell WordPress the widget’s unique ID, its name and its description. Be careful choosing the ID, because for one it should be unique to not conflict with other widgets, and for the other if you want to preserve your widget’s position in the sidebar as well as its settings you will have to bear with that same ID for ever.
Who are you, widget-chan?
1 2 3 4 | function new_widget() { $ops = array('classname' => 'new_widget', 'description' => 'This is your newly created widget!'); parent::WP_Widget('new_widget', $name = 'New widget', $ops); } |
The $ops array contains the options for the widget. ‘Classname’ defines what class attribute the widget’s container will have. And ‘description’ is what help text will be shown to the user on the Widgets page. The first param of the WP_Widget call is the unique ID I talked about before, the $name is the title that will be shown to the user on the Widgets page, and $ops is the call to the options above.
Giving the user a choice
But don’t you think the user should have some voice too? If you want your widget to have customizable options, you will need this function:
1 2 3 4 5 6 | function form($instance) { $title = esc_attr($instance['title']); ?> <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?> <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /></label></p> <?php } |
This is a bit more difficult to explain since it’s a lot of code, but practically, to add new text fields you just have to copy the <p></p> sequence and change ‘title’ to whatever you want. Also, to secure your/user’s database, run the options values through the esc_attribute function like with the $title above.
Saving the customizations
To save the updated options into the database, we need this little function:
1 2 3 | function update($new_instance, $old_instance) { return $new_instance; } |
The widget itself
That allows some filtering in case you want to. But hell, we’ve gone through all these functions, but where’s the actual widget?! This in its most basic form simple function does the magic:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | function widget($args, $instance) { extract($args); echo $before_widget; if(!empty($instance['title'])): echo $before_title . $instance['title'] . $after_title; else: echo $before_title . 'Unnamed widget' . $after_title; endif; echo 'The contents'; echo $after_widget; } |
The two arguments the function takes are the theme-specific settings and the user-customized options from the Widgets page. To simplify their use and refrain from writing e.g. $args[’before_title’], we extract the former, which makes the array accessible via e.g. $before_title. Everything yours must be wrapped inside $before_widget and $after_widget, and the widget’s (in our case the customized) title must be wrapped inside $before_title and $after_title if you don’t want to screw up the sidebar and the theme in general. In our variant of the function, it prints the customized title in case it’s set and our pre-defined one in case not, after what it prints some Hello World text (‘The contents’). Instead of the latter you can do whatever you want. That’s all. Simple, didn’t I say that?
The widget, it exists!
The only thing that’s left to do to have this widget on your Widgets page is to tell WordPress it exists. For that purpose, we have to call an action hook outside of the class like this:
1 | add_action('widgets_init', create_function('', 'return register_widget("new_widget");')); |
There are many variants of that last piece of code in different tutorials, and the most differ in what action hook is actually to be called, but this is the best and right one.
Summary
Now, the code as it should look complete:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | class new_widget extends WP_Widget { function new_widget() { $ops = array('classname' => 'new_widget', 'description' => 'This is your newly created widget!'); parent::WP_Widget('new_widget', $name = 'New widget', $ops); } function widget($args, $instance) { extract($args); echo $before_widget; if(!empty($instance['title'])): echo $before_title . $instance['title'] . $after_title; else: echo $before_title . 'Unnamed widget' . $after_title; endif; echo 'The contents'; echo $after_widget; } function update($new_instance, $old_instance) { return $new_instance; } function form($instance) { $title = esc_attr($instance['title']); ?> <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?> <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /></label></p> <?php } } add_action('widgets_init', create_function('', 'return register_widget("new_widget");')); |
You can change the name of the new_widget class and the new_widget function, but don’t forget to change ‘new_widget’ inside the last action hook call then.

7 Comments
Very nice! I think I’ll be including something similar in Kronblr’s engine (since it is built with WP similarities), as the class extension is the way themes work atm.
This is rather easy, good infos. Maybe I’ll make some interesting things when I find some time. ^^
Ryan A´s last blog post: ごきげんよう
Kroblr? This is how you are gonna name your microblogging engine? Looking forward to it. I find class extensions very comfortable too, and I (aesthetically) like how ‘parent::function’ calls look.^^
Would you like me to write a plugin-from-scratch tutorial? I’m low on post ideas, so if it’s needed I’m gonna pick it up
Yes, Kronblr. There is a github project for it at http://github.com/RyanAltman/kronblr you can check the theme files if you like, very simple theming, plugins too, but no widgets yet.
Plugins aren’t as difficult, it’s pretty straightforward how to make them, but knowing the right hooks can be a pain. Maybe if you go over some of the best hooks O.o Although, most anibloggers don’t mess with plugins/code >_>
Ryan A´s last blog post: Recovery
“Plugins aren’t as difficult, it’s pretty straightforward how to make them, but knowing the right hooks can be a pain. Maybe if you go over some of the best hooks O.o Although, most anibloggers don’t mess with plugins/code >_>”
Ah yeah. This. Gotta agree with this. I don’t know jack shit about CSS. But this rule could be applied to said Ani-bloggers who don’t bother with CSS or creating their own Widgets.
Jubbz´s last blog post: The Decade in Anime: A Retrospect – 2001
Just for polite-correctness, this is not CSS but PHP. CSS is the thing that makes my blog look like it does. PHP does the software core.
Anyway, there are anibloggers who are interested in this, and there are people other than from anisphere who could need this. If you want, you can see this post as a part of the webdesign-o-sphere.
Ah, that’s what you were talking about.
…
I still don’t know jack shit about PHP. –_–
I’m kinda interested in it, but i’m too lazy to put the time and effort into learning.
Jubbz´s last blog post: Biri Biri Show episodes 9 & 10
It’s pretty simple at the beginning, I could post some beginner tutorials.
One Trackback
The simpliest way of creating a widget in WordPress 2.8+…
A simple and explained tutorial of creating widgets in WordPress 2.8+.…