Tell us what you are trying to do?
Independently set the state of each of the checkboxes generated from the generic checkboxes field based on the JSON passed to it via shortcode.
I am passing the generic checkboxes field JSON data to populate them like so:
[cred_generic_field type='checkboxes' field='class-child-list']
{
"options":[ [class_children class="in" output="json"] ]
}
[/cred_generic_field]
NOTE: A lot of the php data handling code has been left out of this example as it's not relevant:
function get_class_children( $atts ) {
//Set the default $atts values
$defaults = array(
'id' => get_the_ID(),
'class' => NULL,
'output' => 'array'
);
//Apply default atts if none have been set
$atts = shortcode_atts( $defaults, $atts );
//Use passed 'ID' parameter if one has been passed
if( $atts['id'] ) {
$class_id = $atts['id'];
}
...
//Initalise array to store results
$child_list = array();
//Loop through each child and setup JSON variables
foreach( $children as $key => $value ) {
//Child post ID as Value and post title (Child full name) as label
$child_list[] = array(
'value' => $value,
'label' => get_the_title( $value )
);
}
//JSON encode the resulting array and trim off the square brackets to Toolset will recognise it
$result = trim( json_encode( $child_list ), '[]' );
}
return $result;
}
add_shortcode("class_children", "get_class_children");
What I need to do now is add some processing so that if the form is opened again, the checkboxes reflect the previous state. This is easy enough to achieve given the post processing that goes on and the way data is updated, but I can't find a way to pass the state to the checkboxes.
Logically, I assumed the 'state' parameter might work, just like with the Types checkboxes field. So that would look something like this (as a test, the real version would be controlled by a variable obviously):
$child_list[] = array(
'value' => $value,
'label' => get_the_title( $value ),
'state' => 'checked'
);
This code doesn't error, but it also doesn't seem to do anything as the checkboxes all remain unchecked by default.
Can someone please advise the correct way of passing this data as there is nothing in the documentation regarding the specifics of the generic fields.
Many thanks.