Sign up ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

In my wp widget, $instance has an attribute called order which will the listing order of libraries/services. I'd like to store a multidimensional associative array that will hold the id of each location/service, its type (i.e. if it's a location or a service), and it's name. I'd like it to look something like this:

     [0] => ( 'id' => 1, 'type' => 'location', 'name' => 'University Library'),
     [1] => ( 'id' => 7, 'type' => 'service', 'name' => 'Circulation Desk') );

Below is the html markup for the wp widget admin pane and $instance['order']:

  <?php _e( 'Select ordering', 'olh' ); ?>:

  <!-- dynamically populated by jQuery -->


  name='<?php echo $this->get_field_name( 'order' ) ?>[]'
  id='<?php echo $this->get_field_id( 'order' ) ?>'
  class='location-service-order' >

    foreach ($instance['order'] as $order ) : ?>

      class="<?php echo $order['class'] ?>"
      value="<?php echo $order['value'] ?>">
      <?php echo $order['name'] ?>
  <?php endforeach; ?>


The user can choose what locations/services s/he wants to display with a multi-select dropdown. Whenever the user selects a library/service, it automatically populates The user can then click the + button of a.add-location-service to add it to ul.location-service-order.

From there, I'd like to save the lis of ul.location-service-order with the attributes I specified above.

Thank you gals/guys for any and all info.

share|improve this question

1 Answer 1

I ended up using a workaround to solve this problem. Instead of attempting to store a multidimensional array as described above, I instead store a simple array of values created by combining the type with the id.

So, instead of:

   [0] => ( 'id' => 1, 'type' => 'location', 'name' => 'University Library'),
   [1] => ( 'id' => 7, 'type' => 'service', 'name' => 'Circulation Desk') );

The array becomes:

array( 'L1', 'S7' );

For the list items, I nested a hidden input inside the list item that will has the name attached and the value I want to store, like so:

    foreach( $instance['order'] as $order ) : 
      if( $order ) : ?>

        <li value='<?php echo $order ?>'>
          <span> Library name. </span>
            name='<?php echo $this->get_field_name( 'order' ) ?>[]'
            value='<?php echo $order ?>'

          <a class='destroy_parent'>X</a>

  endforeach; ?>

Notice the input's name and it's value. The [] at the end of the name indicates that an array of values will be stored.

share|improve this answer

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.