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 from my WP-Polls that displays Polls Widget. It is tested and it works perfectly. Hope it is useful for plugin authors.
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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | <?php ### Class: WP-Polls Widget class WP_Widget_Polls extends WP_Widget { // Constructor function WP_Widget_Polls() { $widget_ops = array('description' => __('Put a poll that you have added in WP-Polls on your sidebar', 'wp-polls')); $this->WP_Widget('polls', __('Polls'), $widget_ops); } // Display Widget function widget($args, $instance) { extract($args); $title = esc_attr($instance['title']); $poll_id = intval($instance['poll_id']); $display_pollarchive = intval($instance['display_pollarchive']); echo $before_widget.$before_title.$title.$after_title; get_poll($poll_id); if($display_pollarchive) { display_polls_archive_link(); } echo $after_widget; } // When Widget Control Form Is Posted function update($new_instance, $old_instance) { if (!isset($new_instance['submit'])) { return false; } $instance = $old_instance; $instance['title'] = strip_tags($new_instance['title']); $instance['poll_id'] = intval($new_instance['poll_id']); $instance['display_pollarchive'] = intval($new_instance['display_pollarchive']); return $instance; } // DIsplay Widget Control Form function form($instance) { global $wpdb; $instance = wp_parse_args((array) $instance, array('title' => __('Polls', 'wp-polls'), 'poll_id' => 0, 'display_pollarchive' => 1)); $title = esc_attr($instance['title']); $poll_id = intval($instance['poll_id']); $display_pollarchive = intval($instance['display_pollarchive']); ?> <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:', 'wp-polls'); ?> <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> <label for="<?php echo $this->get_field_id('display_pollarchive'); ?>"><?php _e('Display Polls Archive Link Below Poll?', 'wp-polls'); ?> <select name="<?php echo $this->get_field_name('display_pollarchive'); ?>" id="<?php echo $this->get_field_id('display_pollarchive'); ?>" class="widefat"> <option value="0"<?php selected(0, $display_pollarchive); ?>><?php _e('No', 'wp-polls'); ?></option> <option value="1"<?php selected(1, $display_pollarchive); ?>><?php _e('Yes', 'wp-polls'); ?></option> </select> </label> <label for="<?php echo $this->get_field_id('poll_id'); ?>"><?php _e('Poll To Display:', 'wp-polls'); ?> <select name="<?php echo $this->get_field_name('poll_id'); ?>" id="<?php echo $this->get_field_id('poll_id'); ?>" class="widefat"> <option value="-1"<?php selected(-1, $poll_id); ?>><?php _e('Do NOT Display Poll (Disable)', 'wp-polls'); ?></option> <option value="-2"<?php selected(-2, $poll_id); ?>><?php _e('Display Random Poll', 'wp-polls'); ?></option> <option value="0"<?php selected(0, $poll_id); ?>><?php _e('Display Latest Poll', 'wp-polls'); ?></option> </select> </label> <input type="hidden" id="<?php echo $this->get_field_id('submit'); ?>" name="<?php echo $this->get_field_name('submit'); ?>" value="1" /> <?php } } ### Function: Init WP-Polls Widget add_action('widgets_init', 'widget_polls_init'); function widget_polls_init() { register_widget('WP_Widget_Polls'); } ?> |



(168 votes, average: 4.03 out of 5)
Lester Chan’s WordPress Plugins June 2009 Update
Posted by Lester Chan at 18:52 in WP-Ban, WP-CommentNavi, WP-DBManager, WP-DownloadManager, WP-EMail, WP-PageNavi, WP-PluginsUsed, WP-Polls, WP-PostRatings, WP-PostViews, WP-Print, WP-RelativeDate, WP-ServerInfo, WP-Stats, WP-Sticky, WP-UserOnlineHere is my June 2009 WordPress plugins update containing all my 16 WordPress plugins update. All of them should work on WordPress 2.8 as I did not test them on any WordPress version below that.
Now my plugins uses jQuery for AJAX instead of TW-Sack. I have also updated the widget code to make use of the new WordPress 2.8 new Widget class and that supports multi-instances widgets. The widget code has now been merge with the main plugin file so the standalone widget plugin file is no longer in use. Please delete the whole plugin folder and upload it again to avoid any error.
As WordPress 2.8 supports loading of JavaScript in the footer, all my plugins’ JavaScripts will be loaded in the footer. Be sure you have in your theme footer.php.
Be sure to read the readme.html and checkout the changelog for more information and most importantly NOTE THE TABS AT THE TOP
WP-Ban 1.50
» Readme/Changelog
» Download Mirror #1
» Support Forum
WP-CommentNavi 1.10
» Readme/Changelog
» Demo
» Download Mirror #1
» Support Forum
WP-DBManager 2.50
» Readme/Changelog
» Download Mirror #1
» Support Forum
WP-DownloadManager 1.50
» Readme/Changelog
» Demo
» Download Mirror #1
» Support Forum
WP-EMail 2.50
» Readme/Changelog
» Demo
» Download Mirror #1
» Support Forum
WP-PageNavi 2.50
» Readme/Changelog
» Demo
» Download Mirror #1
» Support Forum
WP-PluginsUsed 1.50
» Readme/Changelog
» Demo
» Download Mirror #1
» Support Forum
WP-Polls 2.50
» Readme/Changelog
» Demo
» Download Mirror #1
» Support Forum
WP-PostRatings 1.50
» Readme/Changelog
» Demo
» Download Mirror #1
» Support Forum
WP-PostViews 1.50
» Readme/Changelog
» Demo
» Download Mirror #1
» Support Forum
WP-Print 2.50
» Readme/Changelog
» Demo
» Download Mirror #1
» Support Forum
WP-RelativeDate 1.50
» Readme/Changelog
» Demo
» Download Mirror #1
» Support Forum
WP-ServerInfo 1.50
» Readme/Changelog
» Download Mirror #1
» Support Forum
WP-Stats 2.50
» Readme/Changelog
» Demo
» Download Mirror #1
» Support Forum
WP-Sticky 1.50
» Readme/Changelog
» Demo
» Download Mirror #1
» Support Forum
WP-Useronline 2.50
» Readme/Changelog
» Demo
» Download Mirror #1
» Support Forum
If you like or love my plugins a lot, do consider making a donation to me. My Paypal email address is lesterchan AT gmail DOT com. Thank you =D
Tags: 2.8, release5 Comments | 4,987 views