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 some strange trouble with my WordPress today :

I want to store same multiple values in a widget, so I create something like this : myCollection[id][myField] as input's name for my admin widget form.

After a $this->get_field_name call and template integration, it generates the following code :

<label for="widget-pricetable-ibsciss-widget-2-widget-pricetable-ibscisswidget[2][box[1][price]]"> … </label>
<input id="widget-pricetable-ibsciss-widget-2-widget-pricetable-ibsciss-widget[2][box[1][price]]" value="" name="widget-pricetable-ibsciss-widget[2][box[1][price]]"></input>

After the submit I get this instance array from WordPress :

array(4) { [
  "title"]=> string(12) "Global title" 
  ["box[0"]=> array(2) { 
    ["title"]=> string(6) "title1" 
    ["price"]=> string(6) "price1" 
  } 
  ["box[1"]=> array(2) { 
    ["title"]=> string(6) "title2" 
    ["price"]=> string(6) "price2" 
  } 
  ["box_title"]=> string(9) "Box title" 

}

And I have this strange "box[0" keys instead of a well formatted array.

share|improve this question
1  
Sorry, what generates that code? get_field_name looks a bit like ACF code but I'm guessing. –  s_ha_dum Nov 28 '13 at 22:06

1 Answer 1

up vote 1 down vote accepted

Ok, with a quick dirty hack I have achieve my goal :

Instead of myCollection[id][myField] I give to the get_field_name's function something like this : collection][id][field.

So the generated code became :

name="widget-pricetable-ibsciss-widget[2][box][1][title]"

And my array just look perfect now :

array(3) { 
  ["title"]=> string(12) "Global title" 
  ["box"]=> array(2) { 
    [0]=> array(2) { 
      ["title"]=> string(6) "title1" 
      ["price"]=> string(6) "price1" 
    } 
    [1]=> array(2) { 
      ["title"]=> string(6) "title2" 
      ["price"]=> string(6) "price2" 
    } 
  } 
  ["box_title"]=> string(9) "Box title" 
} 

PS : only for information purpose this is the get_field_name method from Wordpress :

function get_field_name($field_name) {
    return 'widget-' . $this->id_base . '[' . $this->number . '][' . $field_name . ']';
}
share|improve this answer

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.