Quantcast
Channel: Wordpress Questions
Viewing all articles
Browse latest Browse all 10

wordpress:Why does WordPress stores widget options in a multidimensional array?

$
0
0

I have a widget with 6 different options. When I use get_option, it gives me a multidimensional array, with all my options inside the inner array, like so:

1
2
$options = get_option('widget_widgetname');
var_dump($options);

And the output for var_dump is:

1
2
3
4
5
6
7
8
9
array(2) { [2]=> array(5)
           {
             ["string"]=> string(6) "Search"
             ["title"]=> string(12) "WDSearchForm"
             ["show_wrapper"]=> string(0) ""
             ["animate"]=> string(0) ""
             ["animateWidth"]=> string(2) "80"
           }
           ["_multiwidget"]=> int(1) }

Therefore, in order to use the options, I would have to do $options[2][“optionName”] rather than the desired $options[“optionName”].
Why does WordPress does that? Is it always going to be under “2”? Is there a better way to retrieve those options?

what's the context in which you need to get the options? if you're using the Widgets API correctly, everything you need is in $instance (which also indirectly answers your question as to why it's a multi-dimensional array).
Can you elaborate on how to do that?
I can't really explain it much better than the default usage example, for example in the widget function, your options are $instance['title'], $instance['animate'], etc..

Answers

Widgets can be used multiple times. Your options will be passed to the widget directly and automatically, as part of the $instance variable. You should not be getting the options directly using get_option.

The widget() function declaration in a WP_Widget derived class looks like this:


function widget( $args, $instance )

The $instance variable will be your options, without the [2]. Each instance of the widget will get its own options.

Similarly, the form() function to display the form for the widget looks like this:


function form( $instance )

Same deal. Finally, the update() function gets two copies of the instanced data, like so:


function update( $new_instance, $old_instance )

The update function should validate the options in $new_instance, and using the options from $old_instance if they are invalid. The function should then return the resulting combined array of the valid options. This will then become the new set of the instanced options for use elsewhere.

Look at the widgets in the core, such as WP_Text_Widget, for examples.

TL;DR: WordPress handles the database stuff for widgets automatically. Don’t call get_option, let the underlying WP_Widget class do the grunt work.

Thank you, @Otto, but what if I want to instantiate the options within a different function, other than form(), update() or widget()?
@WebDeskIL Then that would be a separate question entirely. When you ask your new question, please be sure to explain what you're trying to accomplish by accessing options directly rather than through the Widgets API.

Source


Viewing all articles
Browse latest Browse all 10

Trending Articles