Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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:

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

And the output for var_dump is:

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?

share|improve this question
2  
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). –  Milo Jan 14 '13 at 19:51
    
Can you elaborate on how to do that? –  WebDeskIL Jan 14 '13 at 20:02
1  
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.. –  Milo Jan 14 '13 at 20:06
add comment

1 Answer

up vote 4 down vote accepted

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.

share|improve this answer
1  
Thank you, @Otto, but what if I want to instantiate the options within a different function, other than form(), update() or widget()? –  WebDeskIL Jan 14 '13 at 21:42
    
@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. –  EAMann Jan 14 '13 at 22:25
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.