This support ticket is created vor 6 Jahren, 4 Monaten. There's a good chance that you are reading advice that it now obsolete.
This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.
Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.
Heute stehen keine Supporter zur Arbeit im Werkzeugsatz-Forum zur Verfügung. Sie können gern Tickets erstellen, die wir bearbeiten werden, sobald wir online sind. Vielen Dank für Ihr Verständnis.
Can you tell me how I can programmatically take a checkboxes value and turn it into an array of checked items? Searching around, I can see this is probably done by types_render_field but I'm not sure exactly how to use it. For example...
The implementation of checkboxes fields is... not so nice.
You can get the list of values of checked items as a comma-separated list (using types_render_field with only the field slug and without additional arguments), and then use PHP explode to convert that list into an array (versteckter Link).
That works well if you have stored meaningful values for the checkboxes, e.g. Monday -> monday, Tuesday -> tuesday.
If you just save 1 to denote checked for each option then you are just going to get back a comma-separated list of 1s, which isn't very helpful.
Thanks. Sounds like it should work out. The value of the checkboxes is the integer PHP assigns to that day of the week, so the array should be meaningful.
Regarding the code, can you comment more specifically on how I need to use the types_render_field (line 2 in the code I pasted)? I wasn't sure from the documentation how exactly I should use the function.
It says "With no attributes set, checkboxes will return a comma separated list of values stored for each checked checkbox".
So if you have a checkboxes field with a slug of my-checkboxes then you can assign the comma-separated list of checked items to the variable $list like so:
$list = types_render_field( 'my-checkboxes' );
You can then use explode to convert that string to an array.
Thanks for the help. I was able to get this working, although I had to add some additional information to the code you provided in order to make it work. Here is what I ended up with: