[wp-hackers] New Widgets API

Ryan Boren ryan at boren.nu
Thu Mar 26 23:19:50 GMT 2009


In trunk is a new widgets API.  WP_Widget is a class that abstracts
away much of the complexity involved in writing a widget, especially
multi-widgets. Basically, you extend WP_Widget with your own class
that provides a constructor and three methods -- widget(), form(), and
update().  widget() outputs the actual content of the widget.
update() processes options to be saved.  form() outputs the options
form.  A widget is registered by passing the name of the widget class
to register_widget().  All widgets written with WP_Widget are multiple
instance capable.  Options for old single-instance widgets ported to
WP_Widget will be upgraded to the new multi-option storage format,
which is simply a multi-dimensional array keyed by instance ID.
Options for widgets using the old multi-instance pattern should work
as is.  If your widget has custom option storage needs, you can
provide your own get_settings() and save_settings() methods.

 The WP_Widget source can be viewed here (read the phpdoc for more
info on usage):

http://core.trac.wordpress.org/browser/trunk/wp-includes/widgets.php

You can see examples of how to use it here:

http://core.trac.wordpress.org/browser/trunk/wp-includes/default-widgets.php

If you author any widgets, try porting them to WP_Widget and give your
feedback on what can be improved.  The trac ticket is:

http://core.trac.wordpress.org/ticket/8441

(Props to the MultiWidget class, on which WP_Widget is based:
http://blog.firetree.net/2008/11/30/wordpress-multi-widget/)


More information about the wp-hackers mailing list