There are two ways to precheck a checkbox (which, I should make a third way to be fair)
1 – Use the default
attribute in the form tag
If you click this link, it takes you to the documentation page with a query string in the URL that is being used to define a default checked box in the second demo form. I have a further explanation of that attribute in this support thread you can check out as well.
For what it’s worth, I haven’t quite finished testing this feature, which is why there isn’t a lot of documentation on it yet nor is it yet available in the form tag generator. At any rate, it gets set like the placeholder attribute in that it needs to be encoded in the form tag in order to work within CF7’s shortcode syntax. Actual shortcodes to evaluate dynamic default values work here too.
2 – Set it in the HTML output of your shortcode
Instead of outputting a JSON object, you can output the HTML directly, and you can use my helper functions (specifically the wpcf7dtx_checkbox_group_html() function) to generate the actual HTML so it stays consistent. So where you have:
$options[$results['opt_out']] = 'Opt Out';
return json_encode($options);
You can make it this instead:
$options[$results['opt_out']] = 'Opt Out';
return wpcf7dtx_checkbox_group_html(
array(
'name' => $results['opt_out'], // Name of checkbox input group
'dtx-default' => $results['opt_out'] // Default value
),
$options // Associative array of key/value pairs
);
(please note this is untested, I whipped this up staring at the screen just now)
If you’re unsure what to set dtx-default
to in that function, this is where I’m setting it in the DTX form tag. As you can see, it does all the decoding and then getting the dynamic value of the default
attribute if it’s set. But if you’re generating your options dynamically and want to manually set them in your HTML, that gives you a starting point.
3 – (Coming soon) Something fancy with the JSON maybe?
The JSON output isn’t well documented either, my apologies. Right now, it simply translates key/value -> value/label pairs for options like this:
{
"<whatever value 1>": "<whatever label 1>"
}, {
"<whatever value 2>": "<whatever label 2>"
}
but perhaps I should allow for more flexibility, like:
[
{
"value": "<whatever value 1>",
"label": "<whatever label 1>",
"selected": true
}, {
"value": "<whatever value 2>",
"label": "<whatever label 2>",
"selected": false
}
]
Would this fit your needs?
Hopefully something here answers your question!