Building the WordPress Widget
- An admin component to configure the widget via the “Appearance -> Widget” section of the WordPress blog manager
- The UI component to show the widget to a blog reader
widget, form
and update
. In this example, the widget code is stored in a widget.php
file located in the wp-content/plugins/boobtube
directory and included in the boobtube.php
main plugin file.<?php
class BoobTube_Widget extends WP_Widget {
function BoobTube_Widget() {
$widget_ops = array('classname' => 'boobtube', 'description' => 'My Video');
$control_ops = array('width' => '300', 'height' => '350', 'id_base' => 'boobtube-widget');
$this->WP_Widget('boobtube-widget', 'Boob Tube', $widget_ops, $control_ops);
}
function widget($args, $instance) {
extract($args);
$title = apply_filters('widget_title', $instance['title'] );
// Controlled by theme.
echo $before_widget;
if ($title) {
echo $before_title . $title . $after_title;
}
$bt =& $GLOBALS['boobtube'];
$bt->watch();
// Controlled by theme.
echo $after_widget;
}
function update($new_instance, $old_instance) {
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
return $instance;
}
function form($instance) {
$defaults = array( 'title' => 'My Video');
$instance = wp_parse_args((array) $instance, $defaults);
$title = htmlspecialchars($instance['title']);
echo "<p>n<label for="" . $this->get_field_name('title') . "">Title:</label>n";
echo "n<input type="text" id="" . $this->get_field_id('title') . "" name="" . $this->get_field_name('title') . "" value="" . $title . "" style="width:100%" />n</p>n";
}
}
?>
class BoobTube_Widget extends WP_Widget {
function BoobTube_Widget() {
$widget_ops = array('classname' => 'boobtube', 'description' => 'My Video');
$control_ops = array('width' => '300', 'height' => '350', 'id_base' => 'boobtube-widget');
$this->WP_Widget('boobtube-widget', 'Boob Tube', $widget_ops, $control_ops);
}
widget
method displays the widget on your blog. You must use the $before_widget
, $after_widget
, $before_title
and $after_title
variables because these are controlled by the theme.
function widget($args, $instance) {
extract($args);
$title = apply_filters('widget_title', $instance['title'] );
// Controlled by theme.
echo $before_widget;
if ($title) {
echo $before_title . $title . $after_title;
}
$bt =& $GLOBALS['boobtube'];
$bt->watch();
// Controlled by theme.
echo $after_widget;
}
form
method is where you configure the HTML BoobTube widget. The widget form posts to the update
method described next. Your plugin can be used in multiple widgets, each with customizable features. For BoobTube, you’re updating only the widget title. function form($instance) {
$defaults = array( 'title' => 'My Video');
$instance = wp_parse_args((array) $instance, $defaults);
$title = htmlspecialchars($instance['title']);
echo "<p>n<label for="" . $this->get_field_name('title') . "">Title:</label>n";
echo "n<input type="text" id="" . $this->get_field_id('title') . "" name="" . $this->get_field_name('title') . "" value="" . $title . "" style="width:100%" />n</p>n";
}
update
method takes the form
method data and updates BoobTube’s widget title.
function update($new_instance, $old_instance) {
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
return $instance;
}