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');

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?

  • 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
    Commented Jan 14, 2013 at 19:51
  • Can you elaborate on how to do that? Commented Jan 14, 2013 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
    Commented Jan 14, 2013 at 20:06

1 Answer 1


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.

  • 1
    Thank you, @Otto, but what if I want to instantiate the options within a different function, other than form(), update() or widget()? Commented Jan 14, 2013 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
    Commented Jan 14, 2013 at 22:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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