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 am developing a wordpress widget, and need to store some user configuration values, that is why I am using wordpress update_option and get_option. I am new to widget development.

I have some configuration options for the widget that I need to access from a php function that is called via ajax, that is why I use the Options API (is there another way?)

The code I have is the following (first, widget functions, then my ajax handling function):

public function update( $new_instance, $old_instance ) {

$instance = $old_instance;

$instance = array();
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
$instance['api_key'] = ( ! empty ($new_instance['api_key']) ) ? strip_tags ($new_instance['api_key']) : '';
$instance['forum_key'] = ( ! empty ($new_instance['forum_key']) ) ? strip_tags ($new_instance['forum_key']) : '';
$instance['limit'] = ( ! empty ($new_instance['limit']) ) ? strip_tags ($new_instance['limit']) : '';

return $instance;

} // end widget

public function form( $instance ) {

$instance = wp_parse_args(
    (array) $instance

$title = ( isset ($instance['title'] ) ) ? esc_attr($instance['title']) : __( 'New title', 'eedee-dq-widget' );
$api_key = ( isset ($instance['api_key'] ) ) ? esc_attr($instance['api_key']) : '';
$forum_key = ( isset ( $instance ['forum_key'] ) ) ? esc_attr($instance['forum_key']) : '' ;
$limit = ( isset ( $instance ['limit'] ) ) ? esc_attr($instance['limit']) : 5 ;

// Display the admin form
include( plugin_dir_path(__FILE__) . '/views/admin.php' );

} // end form

public function handle_ajax_post() {

check_ajax_referer( 'my_nonce', 'security' );


    $error = false;

    if ($error == false) {

$options = get_option('widget_my_comment_widget');

everything works (form values saving, ajax calling php function), the only problem is that the line

$options = get_option('widget_my_comment_widget');

returns an array like so

$options = array[3]
    $options[3] = array[4]
        $options[3]['title'] = (string) discussion
        $options[3]['api_key'] = (string) xxx
        $options[3]['forum_key'] = (string) zzz
        $options[3]['limit'] = (string) 5
    $options[4] = array[4]
        $options[4]['title'] = (string) New title
        $options[4]['api_key'] = (string) xxx
        $options[4]['forum_key'] = (string) yyy
        $options[4]['limit'] = (string) 5
    $options['_multiwidget'] = (int) 1

so it returns multiple widget configurations (why? i only have one instance of the widget in my one sidebar? because i have it in multiple themes?). How can I make sure to either get the correct id of the options I want, or make sure there is only one set of options with no id key?

Edit After disabling the plugin, removing the db entry, enabling again, adding widget again the $options look like:

$options = array[2]
    $options[2] = array[4]
        $options[2]['title'] = (string) discourse
        $options[2]['api_key'] = (string) xxx
        $options[2]['forum_key'] = (string) highcut
        $options[2]['limit'] = (string) 5
    $options['_multiwidget'] = (int) 1

so still there is this index, I googled and found it is related to the _multiwidget setting, but why is it there?

Edit 2 My constructor is

    __( 'my Comment Widget', 'eedee-my-widget' ),
        'description'   =>  __( 'Displays recent and popular comments ', 'eedee-my-widget' ), )

so I am not using namespaces (or am I unwillingly?) like noted in this part of the official documentation, I found googling multi-widget.

share|improve this question
Why are you saving to options at all? Are these global values? Typically widget data in stored by the widget's own internal mechanisms-- that $instance variable. –  s_ha_dum Dec 10 '13 at 15:15
Do you mean the update_option line? removed it. Had added it because I thought it would help as I wanted to overwrite the existing data rather than adding, but still if I delete the db entry, delete the line, there are two options sets. do I have to add a unique theme identifier to the option value? is there documentation about it somewhere? –  user9 Dec 10 '13 at 16:54
codex.wordpress.org/Widgets_API#Example_With_Namespaces here, they write about multi-widget, might that be connected, just checking the constructor now –  user9 Dec 10 '13 at 17:17
add comment

1 Answer

I found the answer.

Multiwidget is a nice new feature within wordpress' new widget API. Yet there is sparse documentation on how to use it.

$options = get_option( 'your_option_id' );
if( isset($options) && isset($options[$this->number])) {
  //$this->number returns the unique widget id that corresponds to the database index
  $instance_options = $options[$this->number];
//now use $instance_options['setting']
share|improve this answer
add comment

Your Answer


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.