New widget option after calling register_widget creating empty widget

Description

After I register a new widget class that has never been registered before, navigate to the widgets.php admin page and allow the option to be created automatically. The option value looks like this as serialized data:

a:2:{i:2;a:0:{}s:12:"_multiwidget";i:1;}

As you can see there is an empty widget in there for some reason. Basically the unserialized option value looks like the following as an array:

array( '2' => array(), _multiwidget => 1 );

This is odd behavior since the first multi number has always started at 2, but with that previous array in there it starts at 3, and randomly which is what irks me. i can't seem to find when it happens or a pattern, so naturally this feels like a bug.

As this does not seem to break anything with the core functionality of WordPress, I would say this is normal priority. What this does do is pose many questions for those of us that dive deep into the Widget API, where our plugins and the functionality of them are dependent on how these things work. My widget plugin is quite large, and I need to know if I can depend on the first multi number of a widget when the class is first registered.

If this is not a bug, and normal functionality, I apologize, and would like to ask for a heads up on the subject.

Change History (6)

It looks as though you are the widget master. therefore I wanted to ask you about the plugin of which I spoke about in this report. I am not asking for any beta testing or craziness.

I am not looking for feedback on my plugin, I wanted to show it, my ideas, which essentially is my thought process to maybe give you and anybody else ideas regarding the WordPress Widget API in general.

The plugin is still in beta as I am the only one testing, trying to do a lot with it, clean things up, etc... before a final release. It shouldn't break anything in a blog running on PHP 5.

This seems to be caused by the (tons of) back-compat code in the API. It would be best fixed when we remove most of that code hopefully in 3.1.

However you shouldn't be working directly with the widget option in your plugin. Best is to use the API, it has a lot of hooks everywhere to make that easy. When a user creates and deletes instances of the same widget several times, that number will always increment so the widget option would look something like 5 => array(options...), 9 => array(options...), 15 => array(options...) etc. and some of these instances could be in the inactive widgets area too.

If you really need to access the widget option directly you'll need to check the sidebars_widgets too to determine which are active.