@tessawatkinsllc
Hi Tessa, first of all thanks for your kind and detailed answer. ??
Right after posting, teased by the label If shortcode, it must return only option or optgroup HTML, I started searching the docs and found a couple of useful resources.
I eventually ended up with this somewhat inelegant, temporary solution, even if I actually don’t know if it really works because all of a sudden (probably for a mail server issue), CF7 stopped working (this is NOT related with DTX: uninstalling it and testing a regular CF7 form results in no mail sent anyway):
function au_cf7dtx_userdata_init(){
add_shortcode('au_display_userdata', 'au_cf7dtx_userdata_shortcode');
}
add_action('init', 'au_cf7dtx_userdata_init');
function au_cf7dtx_userdata_shortcode($atts = array(), $content = '', $tag = ''){
$current_user = wp_get_current_user();
$s = get_user_meta($current_user->ID, 'trekkers_data', true) ?? false;
if (!empty($s)) {
$trekkers = $s['trekkers_data']['trekkers'];
$levels = $s['trekkers_data']['levels'];
$one_element = count($levels) === 1 ? 'checked onclick="this.checked = true;"' : '';
$r = "";
for ($i = 0; $i < count($levels); $i++) {
$r .= '<input type="checkbox" name="checktrekkers[]" value="' . $levels[$i] . '" '.$one_element.'><label for="g' . $i . '"> ' . $trekkers[$i] . '</label>';
}
return $r;
} else {
// still dunno
}
}
This could output 1 pre-checked item if there’s only one item in the trekkers array.
Now, even if I still don’t know if this function works, I have two questions:
- if it is possible to completely replace the checkboxes with an hidden field in case there’s only one item in the array: I suppose it is not possible, because if you declare in CF7 you want checkboxes, you can’t magically turn them into hidden fields;
- it should not happen, but it could happen, one thing (check the ‘still dunno’ comment area): what could I do if $s is empty? In that case, is there any way to output something different, eg. a label with a simple warning?
Thanks! ??