Wordpress Multi-Widget

I’ve decided that I hate Wordpress’ multi-widget “API”, so I’ve written a cleaner interface for it: class MultiWidget

Damon Timm added multi-widget functionality to Event-Calendar. He did a fine job, but I was horrified by the hoops he’d been forced to jump through. The barely-documented “API” is so horrible and crufty that I simply don’t trust that it will remain in future versions of Wordpress. I fear that in a few months time, the multi-widget hacks inside wp-admin will change or disappear, and we’d be scrambling to play catch up.

I was all but ready to give up and reluctantly decide against multi-widgets, but then I thought of a better plan…

I’ve taken the example multi-widget code and turned it into an abstract class with a clean interface: class MultiWidget. This class is useless on its own – you have to extend the class to make a custom multi-widget – e.g. class ExampleMultiWidget.

The class ExampleMultiWidget is quite clean, and doesn’t contain any of the crufty secret multi-widget knowledge. I’m hoping that if/when “they” change the multi-widget API, I’ll just be able to take the new example, and translate it into a new version of the interface class MultiWidget, but without having to change any of the derived classes.

Comment · RSS · TrackBack

  1. junsuijin said,

    9 December, 2008 @ 21:20

    Thank you so very much for this. I’ve been meaning to do this myself for some time but have been bogged down in other aspects of creating my new wordpress theme. In fact I wrote a simple multi-widget and abstracted the pseudo-code from there, but hadn’t had time to get the classes actually coded (plus the ugliness of all that multi-widget mumbo jumbo had me somewhat avoiding the task as I’m sure you can relate). My new theme is highly-widgetized with several ’sidebar’ locations, and gives wordpress a ‘booster shot’ of sorts in terms of CMS functionality. I’ll be testing this class out very soon as I plan to use it, or a customized version of it, to quickly create a lot of multi-widgets for my theme. With luck, this will allow for quick creation of easily customizable, multi-instanciable widgets and save some space in the functions file as well!

  2. junsuijin said,

    9 December, 2008 @ 21:32

    Please excuse the double post, but thinking more on the issue, I really must voice my opinion that the WordPress authors should include a class like this in the core distribution. I’m really surprised they’ve gone this long without doing so actually, just as I was surprised some months ago that there was nothing like this even available. Perhaps we can bend some ears and get some attention focused on this issue? Again, thanks for making this available, some people really appreciate such acts of selflessness :D

  3. bforchhammer said,

    12 December, 2008 @ 09:33

    Thanks very much for this class. I’ve refactored our Author Avatars Plugin to use this class, it works very nicely and makes creating multi-widgets so much easier…

    I overwrote one method (get_field_id) to clean up the id used on my the html form elements and remove any invalid characters (see http://ec3.pastebin.com/f566bdda2). Maybe that’s something that you’d want to add to your base class…?

  4. inportb said,

    15 December, 2008 @ 04:18

    Oh man… that is one elegant solution. Thank you so much for sharing your work! It has saved me a bunch of time.

  5. Dan Cole said,

    4 January, 2009 @ 00:17

    This script creates a wrong data type error in some cases, due to the second argument in: if( !in_array( $this->id_base.'-'.$number, $_POST['widget-id'] ) ) , which is within the control_callback function. This happens when removing a multi-widget in WordPress 2.7.

    I’m not sure if $_POST['widget-id'] would ever be an array. I patched my copy by adding (array) before $_POST['widget-id'] to force it to be an array.

    Anyway, thanks for the script… it does make multi-widget creating easier.

  6. Tim said,

    8 January, 2009 @ 14:16

    Big time saver – thanks!

  7. Christian said,

    14 January, 2009 @ 14:59

    Thanks for the script! I implemented my extended pages widget in multi in 5 minutes and it works very well. The only problem I found is a

    PHP Warning:  Cannot modify header information -headers already sent by
    (output started at wp-content/plugins/multi-pages-extended/multi_pages_extended.php:515)
    in wp-includes/pluggable.php on line 850
    On the row 515 I simply declare:
    $pages_extended_multi = new PagesExtendedWidget();
    and as I am not a PHP expert, I don’t understand the problem

  8. alex said,

    4 February, 2009 @ 16:37

    Christian – you have probably got stray whitespace outside of your <?php and ?> tags. They must be at the very start and the very end of each file.

  9. zerone said,

    15 March, 2009 @ 01:26

    Christian not just what alex has mentioned, also make sure you set you FTP client to binary mode as a measure.

  10. John Kemp said,

    18 March, 2009 @ 17:33

    Alex,

    Thanks for the work you’ve done encapsulating this functionality. I’ve been playing with this and I think I’m just about there. However, I’m getting a 500 internal error, believing the cause to be that wp or php can’t find the MultiWidget class. Where is the multiwidget.php file supposed to go? I currently have it in the includes folder.

    Thanks for any insight you can provide.

  11. bforchhammer said,

    31 March, 2009 @ 12:48

    For reference… this has found its way into the wordpress core for 2.8! (see trac ticket: http://core.trac.wordpress.org/ticket/8441)

    Congrats!

  12. alex said,

    25 February, 2010 @ 23:26

    I notice that they’ve incorporated it into Wordpress without attribution. Sigh.

Leave a Comment

Sponsors