{"id":252,"date":"2009-03-17T13:30:41","date_gmt":"2009-03-17T05:30:41","guid":{"rendered":"http:\/\/lesterchan.net\/wordpress\/?p=252"},"modified":"2009-06-16T12:10:12","modified_gmt":"2009-06-16T04:10:12","slug":"new-wp_widget-class-in-wordpress-28","status":"publish","type":"post","link":"https:\/\/lesterchan.net\/wordpress\/2009\/03\/17\/new-wp_widget-class-in-wordpress-28\/","title":{"rendered":"New WP_Widget Class In WordPress 2.8"},"content":{"rendered":"<p>In WordPress 2.8, there is a new WP_Widget class, which personally I like it very much as now the multi-instances of widgets is handled by WordPress, all you need to do is just to extends the WP_Widget class and overwrite 3 of the functions namely widgets(), update() and form();<\/p>\n<p>Below is a sample code taken from my WP-Polls that displays Polls Widget. It is tested and it works perfectly. Hope it is useful for plugin authors.<\/p>\n<pre lang=\"php\" line=\"1\">\r\n<?php\r\n### Class: WP-Polls Widget\r\n class WP_Widget_Polls extends WP_Widget {\r\n\t\/\/ Constructor\r\n\tfunction WP_Widget_Polls() {\r\n\t\t$widget_ops = array('description' => __('Put a poll that you have added in WP-Polls on your sidebar', 'wp-polls'));\r\n\t\t$this->WP_Widget('polls', __('Polls'), $widget_ops);\r\n\t}\r\n\r\n\t\/\/ Display Widget\r\n\tfunction widget($args, $instance) {\r\n\t\textract($args);\r\n\t\t$title = esc_attr($instance['title']);\r\n\t\t$poll_id = intval($instance['poll_id']);\r\n\t\t$display_pollarchive = intval($instance['display_pollarchive']);\r\n\t\techo $before_widget.$before_title.$title.$after_title;\r\n\t\tget_poll($poll_id);\r\n\t\tif($display_pollarchive) {\r\n\t\t\tdisplay_polls_archive_link();\r\n\t\t}\r\n\t\techo $after_widget;\r\n\t}\r\n\r\n\t\/\/ When Widget Control Form Is Posted\r\n\tfunction update($new_instance, $old_instance) {\r\n\t\tif (!isset($new_instance['submit'])) {\r\n\t\t\treturn false;\r\n\t\t}\r\n\t\t$instance = $old_instance;\r\n\t\t$instance['title'] = strip_tags($new_instance['title']);\r\n\t\t$instance['poll_id'] = intval($new_instance['poll_id']);\r\n\t\t$instance['display_pollarchive'] = intval($new_instance['display_pollarchive']);\r\n\t\treturn $instance;\r\n\t}\r\n\r\n\t\/\/ DIsplay Widget Control Form\r\n\tfunction form($instance) {\r\n\t\tglobal $wpdb;\r\n\t\t$instance = wp_parse_args((array) $instance, array('title' => __('Polls', 'wp-polls'), 'poll_id' => 0, 'display_pollarchive' => 1));\r\n\t\t$title = esc_attr($instance['title']);\r\n\t\t$poll_id = intval($instance['poll_id']);\r\n\t\t$display_pollarchive = intval($instance['display_pollarchive']);\r\n?>\r\n\r\n\r\n\t\t\t<label for=\"<?php echo $this->get_field_id('title'); ?>\"><?php _e('Title:', 'wp-polls'); ?>\r\n<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>\r\n\t\t\r\n\r\n\r\n\t\t\t<label for=\"<?php echo $this->get_field_id('display_pollarchive'); ?>\"><?php _e('Display Polls Archive Link Below Poll?', 'wp-polls'); ?>\r\n<select name=\"<?php echo $this->get_field_name('display_pollarchive'); ?>\" id=\"<?php echo $this->get_field_id('display_pollarchive'); ?>\" class=\"widefat\">\r\n\t\t\t\t\t<option value=\"0\"<?php selected(0, $display_pollarchive); ?>><?php _e('No', 'wp-polls'); ?><\/option>\r\n\t\t\t\t\t<option value=\"1\"<?php selected(1, $display_pollarchive); ?>><?php _e('Yes', 'wp-polls'); ?><\/option>\r\n\t\t\t\t<\/select>\r\n\r\n\t\t\t<\/label>\r\n\t\t\r\n\r\n\r\n\t\t\t<label for=\"<?php echo $this->get_field_id('poll_id'); ?>\"><?php _e('Poll To Display:', 'wp-polls'); ?>\r\n<select name=\"<?php echo $this->get_field_name('poll_id'); ?>\" id=\"<?php echo $this->get_field_id('poll_id'); ?>\" class=\"widefat\">\r\n\t\t\t\t\t<option value=\"-1\"<?php selected(-1, $poll_id); ?>><?php _e('Do NOT Display Poll (Disable)', 'wp-polls'); ?><\/option>\r\n\t\t\t\t\t<option value=\"-2\"<?php selected(-2, $poll_id); ?>><?php _e('Display Random Poll', 'wp-polls'); ?><\/option>\r\n\t\t\t\t\t<option value=\"0\"<?php selected(0, $poll_id); ?>><?php _e('Display Latest Poll', 'wp-polls'); ?><\/option>\r\n\t\t\t\t<\/select>\r\n\r\n\t\t\t<\/label>\r\n\t\t\r\n<input type=\"hidden\" id=\"<?php echo $this->get_field_id('submit'); ?>\" name=\"<?php echo $this->get_field_name('submit'); ?>\" value=\"1\" \/>\r\n<?php\r\n\t}\r\n}\r\n\r\n### Function: Init WP-Polls Widget\r\nadd_action('widgets_init', 'widget_polls_init');\r\nfunction widget_polls_init() {\r\n\tregister_widget('WP_Widget_Polls');\r\n}\r\n?>\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In WordPress 2.8, there is a new WP_Widget class, which personally I like it very much as now the multi-instances of widgets is handled by WordPress, all you need to do is just to extends the WP_Widget class and overwrite 3 of the functions namely widgets(), update() and form(); Below is a sample code taken &hellip; <a href=\"https:\/\/lesterchan.net\/wordpress\/2009\/03\/17\/new-wp_widget-class-in-wordpress-28\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;New WP_Widget Class In WordPress 2.8&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,19],"tags":[113,67],"class_list":["post-252","post","type-post","status-publish","format-standard","hentry","category-plugins","category-wordpress","tag-113","tag-widget"],"views":34076,"_links":{"self":[{"href":"https:\/\/lesterchan.net\/wordpress\/wp-json\/wp\/v2\/posts\/252","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lesterchan.net\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lesterchan.net\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lesterchan.net\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lesterchan.net\/wordpress\/wp-json\/wp\/v2\/comments?post=252"}],"version-history":[{"count":0,"href":"https:\/\/lesterchan.net\/wordpress\/wp-json\/wp\/v2\/posts\/252\/revisions"}],"wp:attachment":[{"href":"https:\/\/lesterchan.net\/wordpress\/wp-json\/wp\/v2\/media?parent=252"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lesterchan.net\/wordpress\/wp-json\/wp\/v2\/categories?post=252"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lesterchan.net\/wordpress\/wp-json\/wp\/v2\/tags?post=252"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}