0

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.

1
  • 1
    Sorry, what generates that code? get_field_name looks a bit like ACF code but I'm guessing. Commented Nov 28, 2013 at 22:06

1 Answer 1

1

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 . ']';
}

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.