• islp

    (@islp)


    Hello, I just ask a question to know if your plugin is right for me.

    With a plugin of mine I register some user data as user meta. The data are arrays of arrays.

    I would like to use the data to dynamically display checkboxes in the form. The number of checkboxes is variable and depends on the data associated with the users (a user can have 1 to n checkboxes associated).

    When the checkbox to display is only one, possibly the checkbox should appear checked and not clickable or, better, it should convert to a hidden field.

    Is it possible to do this with your plugin?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Tessa (they/them), AuRise Creative

    (@tessawatkinsllc)

    Yep! You’ll want to use the dynamic checkbox for this task. You’ll need two custom shortcodes with the first one being the one that identifies the pre-checked option(s) and use that shortcode in the Selected Default option (default attribute in form tag); example here.

    The second shortcode will be to set up the options. For the example above, I’d recommend the “return options as JSON” method.

    For security purposes, you’ll need to add your custom meta key on the user object to the allow list (info on that).

    Let me know if you need more help ??

    Thread Starter islp

    (@islp)

    @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:

    1. 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;
    2. 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! ??
    Thread Starter islp

    (@islp)

    @tessawatkinsllc

    I can’t edit my previous message: just a couple of lines to tell you my solution didn’t work. In my CF Mail tab, I have [dynamic_checkbox-65], but, if I send a form with only a regular field and this dynamic checkbox, I get the value coming from the field and nothing from the checkboxes (the display part is ok, that is: checkbox are dynamically displayed).

    [EDIT] By replacing the name attribute of the checkboxes (turning it into dynamic_checkbox-65), values are correctly sent. ?? I perfectly know this is far from being perfect, any comment is appreciated (besides the ones I red, but still not implemented)

    Cheers

    • This reply was modified 2 weeks, 6 days ago by islp.
Viewing 3 replies - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.