PHP Code:
<?php
class WP_Widget_Basic extends WP_Widget {
function WP_Widget_Basic() {
$widget_ops = array(
'classname' => 'widget_basic',
'description' => 'Basic Widget, no options except title' );
$this->WP_Widget('basic', 'Basic', $widget_ops);
}
The above basically just defines the widget to WordPress. It gives it a classname for display, and a description to show in the back end. It also gives it a name "Basic" and an internal identifier "basic".
PHP Code:
function widget( $args, $instance ) {
extract($args);
$title = apply_filters('widget_title', empty($instance['title']) ? 'Basic' : $instance['title']);
echo $before_widget;
if ( $title )
echo $before_title . $title . $after_title;
?>
<p> BASIC! </p>
<?php
echo $after_widget;
}
Now we have the guts of the widget. This displays the actual content onto the sidebar. First, it extracts the arguments from the widget system. These are things like before_widget, after_widget, before_title, after_title. Basically, these are defined by the declaration of the widget sidebars in the theme. The widget uses them in order to conform to the way the theme wants it to behave.
Next, it checks for the title of the widget in the instance. This is a multi-widget, there can be more than one of it. Each copy of the widget is a separate instance, and this instance of it is passed in.
Finally, it outputs the widget itself. The before, the title, the code, then the after. Easy.
PHP Code:
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
return $instance;
}
This is the update function, which receives the data passed to it from the configuration form. It gets a copy of the new data in new_instance and a copy of what the data used to be in old_instance. Here, it takes the old data, replaces the title with the title recieved from the form, then returns the changed instance. Each form input should be processed similarly.
PHP Code:
function form( $instance ) {
$instance = wp_parse_args( (array) $instance, array( 'title' => '' ) );
$title = strip_tags($instance['title']);
?>
<p><label for="<?php echo $this->get_field_id('title'); ?>">Title:</label> <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>" /></p>
<?php
}
Finally, we have the form itself. It receives a copy of the instance, which it cleans up a bit and uses to populate the form. You can see that building the form uses the get_field_id and get_field_title functions, this is to create the proper ids and names for form elements so that the data, when saved, comes back to the correct instance of the widget. The form itself, you'll note, has no extra formatting. The only thing used is the class of "widefat" on the input. This allows the natural styling of the widget admin page to control the display of the form elements, and lets it fit inside the box.
If you were to add more form elements, you'd do three things:
1. Add the input into the form function.
2. Process the input in the update function (clean it up, put it into the instance).
3. Use it in the widget function (get it from the instance, echo it, or whatever).