Site icon Hip-Hop Website Design and Development

WordPress Widget – Saving multidimensional arrays into $instance

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:

 array(
     [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']:

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

<select
  name='select-hours-order'
  class='select-hours-order'>
  <!-- dynamically populated by jQuery -->
</select>

<a 
  class='add-location-service'>
  <i>+</i>
</a>

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

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

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

</ul>

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 select.select-hours-order. 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.