Tagged: checkboxes, metabox
- This topic has 5 replies, 2 voices, and was last updated 7 years, 1 month ago by
Steve.
-
AuthorPosts
-
-
January 8, 2015 at 2:12 pm #3167
angeliqueMemberHello,
I’m using PikList for WordPress and for some reason, I’m having trouble to add checkboxes and produce a clean output.
Basically, I created a meta-box “purchase-links” to a custom type “Books”. It looks like this:
My code looks like this:piklist('field', array( 'type' => 'group' ,'field' => 'purchases' ,'columns' => '12' ,'label' => __('Purchase links') ,'fields' => array( array( 'type' => 'group' ,'field' => 'shop' ,'add_more' => true ,'fields' => array( array( 'type' => 'select' ,'field' => 'shop_id' ,'value' => 'amazon' ,'columns' => '12' ,'attributes' => array( 'class' => 'text' ) ,'choices' => array( 'amazon' => 'Amazon' ,'kobo' => 'Kobo' ,'chapters' => 'Chapters Indigo' ) ) ,array( 'type' => 'checkbox' ,'field' => 'format' ,'columns' => '12' ,'attributes' => array( 'class' => 'text' ) ,'choices' => array( 'paper' => 'paper' ,'ebook' => 'ebook' ,'aubiobook' => 'audiobook' ) ) ,array( 'type' => 'text' ,'field' => 'url' ,'label' => 'URL' ,'columns' => '12' ,'attributes' => array( 'placeholder' => 'URL' ) ) ) ) ) ));It kind of works, but I don’t really understand the output it produces for the checkboxes. There’s an extra “array”, and I don’t understand why there is null entries in those arrays. Here is the r_print() of a “shop”:
Array ( [shop_id] => Array ( [0] => amazon ) [format] => Array ( [0] => Array ( [0] => paper [1] => [2] => ebook [3] => aubiobook ) ) [url] => Array ( [0] => http://www.amazon.ca/Pride-Prejudice-Jane-Austen/dp/0486284735/ref=sr_1_1?ie=UTF8&qid=1420671439&sr=8-1&keywords=jane+austen+pride+and+prejudice ) ) Array ( [shop_id] => Array ( [0] => kobo ) [format] => Array ( [0] => Array ( [0] => [1] => ebook ) ) [url] => Array ( [0] => http://store.kobobooks.com/en-CA/ebook/pride-and-prejudice-84 ) ) Array ( [shop_id] => Array ( [0] => chapters ) [format] => Array ( [0] => Array ( [0] => paper [1] => [2] => ebook [3] => aubiobook ) ) [url] => Array ( [0] => http://www.chapters.indigo.ca/books/pride-and-prejudice-penguin-classics/9780143105428-item.html?ikwsec=Home&ikwidx=1 ) )Did I do something wrong that it produces this extra array and null entries for the checkboxes (“format” field)?
And in general, is it normal that Piklist produces so many arrays or is it something I do wrong? For a simple text field (like “url”), I didn’t expect it to produce an array containing one entry. Or is each metabox field contained in an array anyway?Thank you for your help, and don’t hesitate to ask me if I’m not too clear.
Angélique
-
January 8, 2015 at 4:20 pm #3170
SteveKeymaster@angelique– Welcome to the Piklist community!
It looks like you have a group within a group… which I doubt you need. What is the field format you are looking for? What should be in the add-more?
-
January 8, 2015 at 4:36 pm #3171
angeliqueMemberI’d like to achieve a list of ‘shops’ where each contains the three following fields:
1) a select field ‘shop_id’
2) a set of three checkboxes (ebook, paper, audiobook): the ‘format’
3) a text field ‘url’You can add as many ‘shop’ as you want and all three fields are required for each new shop.
-
January 8, 2015 at 5:07 pm #3172
SteveKeymaster@angelique– The good news about Piklist group/add-more fields is that they are extremely flexible and powerful, with unlimited configuration options. The bad news is that the arrays can get confusing. We are totally aware of that and will resolve this in an upcoming version of Piklist.
For now, one of our Piklist Pros, @jasontheadams, wrote an awesome function you can use that makes things more managable. Try the
parse_arrayfunction listed on his github page.The empty or undefined array keys are a bug and will also be resolved in the next version of Piklist.
Also, I don’t think you need the group within a group, so I simplified your code:
piklist('field', array( 'type' => 'group' ,'field' => 'shop' ,'add_more' => true ,'label' => __('Purchase links') ,'fields' => array( array( 'type' => 'select' ,'field' => 'shop_id' ,'value' => 'amazon' ,'columns' => '12' ,'attributes' => array( 'class' => 'text' ) ,'choices' => array( 'amazon' => 'Amazon' ,'kobo' => 'Kobo' ,'chapters'=> 'Chapters Indigo' ) ) ,array( 'type' => 'checkbox' ,'field' => 'format' ,'columns' => '12' ,'attributes' => array( 'class' => 'text' ) ,'choices' => array( 'paper' => 'paper' ,'ebook' => 'ebook' ,'aubiobook' => 'audiobook' ) ) ,array( 'type' => 'text' ,'field' => 'url' ,'label' => 'URL' ,'columns' => '12' ,'attributes' => array( 'placeholder' => 'URL' ) ) ) ));Let us know if this works for you.
-
January 8, 2015 at 5:24 pm #3173
angeliqueMemberOh, ok! It works perfectly, thank you very much!
And now I understand why the arrays : all entries info are aggregated by fields. I was thinking the other way around, more object-oriented of json-like, where each entry would contain its own info.
It’s crystal clear, now, thank you very much for your help! -
January 8, 2015 at 5:27 pm #3174
SteveKeymasterGreat! Let us know if you need any more help.
-
-
AuthorPosts
- You must be logged in to reply to this topic.