New WP_Widget Class In WordPress 2.8

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.

 __('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']);
?>


			
		


			
		


			
		


25 thoughts on “New WP_Widget Class In WordPress 2.8

  1. Brian Meagher

    Lester, can you explain what the new WP_widget class means to us non-developers?
    What can the end user expect to see when this class gets implemented?

  2. Lester Chan Post author

    For non-developers using WP, you can except multi-instances of all widgets even those plugin that has widgets provided that the plugin author has updated the code.

    Taking WP-Polls as an example, you can have 10 Polls Widget now instead of the usual 1.

  3. Pingback: WordPress News Report: iPhone, Widgets API, WordCamps in Hong Kong and China, International WordPress.tv « Lorelle on WordPress

  4. Pingback: WordPress Plugins News: Coffee2Code Plugin Marathon, Plugin Podcast, Plugin Developer Center, Plugin Checklist « Lorelle on WordPress

  5. Pingback: WordPress News: Widget API, Bye-Bye Kubrick, Query_Posts Plugin, and Template Tag Plugin | The Blog Herald

  6. Pingback: WordPress News Report: iPhone, Widgets API, WordCamps in Hong Kong and China, International WordPress.tv | linkfeedr

  7. Pingback: WordPress Plugins News: Coffee2Code Plugin Marathon, Plugin Podcast, Plugin Developer Center, Plugin Checklist | linkfeedr

  8. Harsh Agrawal

    Just saw this update on Twitter and seems like wordpress is keeping up its promise by giving the best of feature with every new release.

  9. Pingback: WordPress 2.8 for theme authors | The Theme Museum

  10. Leon

    Hi
    Thanks for your guide. I have followed your guide and managed to create multiple instances of the plug-in and had then displayed them on different sidebars.
    But when I try to change the options on one widget on a sidebar … the changes will automatically apply to all the instances of widgets on all other sidebars!?
    Do you have an idea on how to allow the plug-in handle multiple instances of widgets? Or …
    Anyway, I have learnt a lot for the first time about Widget once read this guide. Thanks again
    Leon

  11. Romain

    You should change `$title = attr($instance[‘title’]);` into `$title = esc_attr($instance[‘title’]);` on line 13.

    This issue could confuse some plugin developer.

  12. Pingback: » WordPress Plugins News: Coffee2Code Plugin Marathon, Plugin Podcast, Plugin Developer Center, Plugin Checklist

  13. Randy

    Lester,

    I’ve zipped the file and uploaded it using the 2.8.1 plugin page on my site. It says it uploaded successfully but I’m getting the error ‘plugin does not have a valid header’ when I try to activate it. Do you know what this is?

  14. Pingback: New And Comprehensive Wordpress 2.8 Tutorial and Hack Toolbox | Graphic and Web Design Blog - Inspiration, Resources and Tools

  15. Pingback: New WP_Widget Class In WordPress 2.8 « Lester Chan’s WordPress Plugins | WpMash - WordPress News

  16. Jodi

    Lester — thank you!! I have been all over the Internet and tried many tutorials for adding a WP2.8 widget to my plugin and nothing worked until I found your post. Thank you for sharing your code. It has helped me tremendously!

  17. Ahmed

    LC, I am impressed. Speechless for this tutorial, I’ve been able to convert to this method and it saves time and code and gives us developers and blog users great flexibility. Thanks! Searched the whole Google but didnt find something close to this tutorial. Its straightforward and simple.

Comments are closed.