Video Tutorial Creating a widget
In addition to the widgets proposed by default it is also possible to create your own widgets which can then be placed in the different sidebars of our theme. To create a widget just create a class that will extend the class WP_Widget
of WordPress.
This class must contain several methods
- widget, will display the HTML rendering of the widget
- form, will display the form for the administration of the widget
- update, will return an array of attributes to save for the current Widget.
<? php
class YoutubeWidget extends WP_Widget {
public function __construct ()
{
parent :: __ construct ('youtube_widget', 'Youtube Widget');
}
public function widget ($ args, $ instance) {
echo $ args ('before_widget');
if (isset ($ instance ('title'))) {
$ title = apply_filters ('widget_title', $ instance ('title'));
echo $ args ('before_title'). $ title. $ args ('after_title');
}
$ youtube = isset ($ instance ('youtube'))? $ instance ('youtube'): '';
echo '';
echo $ args ('after_widget');
}
public function form ($ instance) {
$ title = isset ($ instance ('title'))? $ instance ('title'): '';
$ youtube = isset ($ instance ('youtube'))? $ instance ('youtube'): '';
?>
<label for = "= $this->get_field_id ('title')?> "> Title
<input
class = "widefat"
type = "text"
name = "= $this->get_field_name ('title')?> "
value = "= esc_attr($title) ?>"
id = "= $this->get_field_name ('title')?> ">
<label for = "= $this->get_field_id ('youtube')?> "> Id Youtube
<input
class = "widefat"
type = "text"
name = "= $this->get_field_name ('youtube')?> "
value = "= esc_attr($youtube) ?>"
id = "= $this->get_field_name ('youtube')?> ">
$ newInstance ('title'), 'youtube' => $ newInstance ('youtube'));
}
}