• Resolved db03

    (@db03)


    Hi,

    I encountered difficulties updating my field values, so I decided to investigate.

    It seems that the configuration of radio buttons, checkboxes, and possibly other elements is opposite to the normal behavior in Contact Form 7. For example, when using options, a key|label pair is expected (e.g., DTX), but with Contact Form 7, it is the opposite: value|key. Consequently, when I try to save the values in the database through an automator, it saves the key instead.

    I would appreciate any assistance you can provide.

    Thank you.

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

    (@tessawatkinsllc)

    Do you have an example of where the error is presenting? The code was written to be consistent with key|label.

    See this Contact Form 7 documentation regarding accessing user input data. If you’re hooked into wpcf7_before_send_mail, the value you’re looking for could be in $_POST (returns the keys) or $posted_data (returns the labels). I resolved a similar issue recently also worth taking a look at.

    Let me know if this helps!

    Plugin Author Tessa (they/them), AuRise Creative

    (@tessawatkinsllc)

    Also, running a quick test, I realised you’re right! DTX does it backwards from CF7 and that’s a huge oversight on my part. One moment more…

    Plugin Author Tessa (they/them), AuRise Creative

    (@tessawatkinsllc)

    Okay, if the above info doesn’t help with your automation and you’re using DTX shortcodes in the form tags, I’d recommend using our custom wpcf7dtx_shortcode filter to return the values you need, you can check out it’s documentation here.

    Thread Starter db03

    (@db03)

    Thank you for your time. I’ve tried several different approaches, but I can’t seem to get it to work! Could you provide more detailed guidance? I’ve attempted to use add_shortcode, add_action, and add_filter, but I’m unsure of where to trigger them since my form is only intended to update user meta values and skip_mail is enabled.

    i have tried this and yet no result, it seems no key|value pair is sent only the value:

    I would appreciate any assistance you can provide.

    Thank you.

    • This reply was modified 12 months ago by db03. Reason: didnt paste the right code
    Thread Starter db03

    (@db03)

    here is the code, sorry!

     add_action( 'wpcf7_before_send_mail', 'switch_options_key_value' );
    
     function switch_options_key_value( $contact_form ) {
        $submission = WPCF7_Submission::get_instance();
        if ( $submission ) {
            $posted_data = $submission->get_posted_data();
            //var_dump($posted_data);
            if ( isset( $posted_data['boutique_en_ligne'] ) ) {
                $selected_key = $posted_data['boutique_en_ligne']; 
                $options = get_your_options_array( $posted_data );
                if ( $options !== false ) {
                    $flipped_options = array_flip( $options );
    
                    if ( isset( $flipped_options[ $selected_key ] ) ) {
                        $selected_value = $flipped_options[ $selected_key ];
                        
                        // Save the selected value to user meta
                        update_user_meta( get_current_user_id(), 'boutique_en_ligne', $selected_value );
                    } else {
                        error_log( 'Selected key not found in options array' );
                    }
                } else {
                    error_log( 'Error parsing options array' );
                }
            } else {
                error_log( 'boutique_en_ligne field not found in posted data' );
            }
        } else {
            error_log( 'Submission instance not found' );
        }
    }
    
    function get_your_options_array( $posted_data ) {
        $dynamic_options = array();
    
        if ( isset( $posted_data['boutique_en_ligne'] ) ) {
            if ( is_array( $posted_data['boutique_en_ligne'] ) ) {
                $field_value = implode(',', $posted_data['boutique_en_ligne']);
            } else {
                $field_value = $posted_data['boutique_en_ligne'];
            }
    
            error_log( 'Field value before explode: ' . $field_value );
    
            $pairs = explode( ',', $field_value );
    
            error_log( 'Pairs after explode: ' . print_r( $pairs, true ) );
    
            foreach ( $pairs as $pair ) {
                $parts = explode( ':', $pair );
                if ( count( $parts ) == 2 ) {
                    $dynamic_options[ $parts[0] ] = $parts[1];
                } else {
                    error_log( 'Error parsing pair: ' . $pair );
                    return false; // Error parsing options array
                }
            }
        } else {
            return false; // boutique_en_ligne field not found
        }
    
        return $dynamic_options;
    }
    Plugin Author Tessa (they/them), AuRise Creative

    (@tessawatkinsllc)

    Okay, try this snippet and tell me what shows up in your debug.log (without PII of course)

    add_action('wpcf7_before_send_mail', 'switch_options_key_value', 10, 3);
    function switch_options_key_value($contact_form, $abort, $submission)
    {
        $posted_data = $submission->get_posted_data();
        if (array_key_exists('boutique_en_ligne', $posted_data)) {
            $selected_key = $posted_data['boutique_en_ligne'];
            $options = get_your_options_array($posted_data, $_POST); // Also send the values of $_POST
            if ($options !== false) {
                // Remove unnecessary flipping
                if (array_key_exists($selected_key, $options)) {
                    $selected_value = $options[$selected_key];
                    // Save the selected value to user meta
                    update_user_meta(get_current_user_id(), 'boutique_en_ligne', $selected_value);
                } else {
                    error_log('Selected key not found in options array');
                }
            } else {
                error_log('Error parsing options array');
            }
        } else {
            error_log('boutique_en_ligne field not found in posted data');
        }
    }
    
    function get_your_options_array($posted_data, $post_data)
    {
        if (array_key_exists('boutique_en_ligne', $posted_data) && array_key_exists('boutique_en_ligne', $post_data)) {
            error_log('Posted Data: ' . print_r($posted_data['boutique_en_ligne'], true)); // Holds the "labels"
            error_log('Post Data: ' . print_r($post_data['boutique_en_ligne'], true)); // Holds the raw data
    
            $posted_pairs = array();
            $post_pairs = array();
    
            // Remove unnecessary imploding
            if (is_array($posted_data['boutique_en_ligne'])) {
                $posted_pairs = $posted_data['boutique_en_ligne'];
            } elseif (is_string($posted_data['boutique_en_ligne'])) {
                $posted_pairs = explode(',', $posted_data['boutique_en_ligne']);
            }
            if (is_array($post_data['boutique_en_ligne'])) {
                $post_pairs = $post_data['boutique_en_ligne'];
            } elseif (is_string($post_data['boutique_en_ligne'])) {
                $post_pairs = explode(',', $post_data['boutique_en_ligne']);
            }
    
            error_log('Posted Pairs: ' . print_r($posted_pairs, true));
            error_log('Post Pairs: ' . print_r($post_pairs, true));
    
            if (count($posted_pairs) === count($post_pairs)) {
                $dynamic_options = array();
                // Does looping these $post_pairs give more consistent results?
                // foreach ($posted_pairs as $pair) {
                foreach ($post_pairs as $pair) {
                    /**
                     * Based on your description, looping $posted_pairs yields this:
                     *
                     * $pair = "label:value" for CF7 fields
                     * $pair = "value:label" for DTX fields
                     */
                    $parts = explode(':', $pair);
                    if (count($parts) == 2) {
                        // Instead of flipping later, just set it correctly here
                        $dynamic_options[$parts[1]] = $parts[0];
                    } else {
                        error_log('Error parsing pair: ' . $pair);
                        return false; // Error parsing options array
                    }
                }
                return count($dynamic_options) > 0 ? $dynamic_options : false;
            } else {
                error_log('The pair counts of boutique_en_ligne do not match!');
            }
        }
        return false; // boutique_en_ligne field not found
    }

    In the newer versions of Contact Form 7, the wpcf7_before_send_mail hook can pass up to 3 parameters, with the current $submission instance as the third parameter, so you can access it directly instead of using the WPCF7_Submission class.

    I also send the $_POST array to the get_your_options_array() function. I’m curious if it will provide you more consistent results? It passes the data found in the value="{this value}" part of the checkbox, so even if you have a CF7 checkbox configured like this:

    [checkbox boutique_en_ligne "Foo | bar"]

    Or a DTX checkbox like this (with the values swapped)

    [dynamic_checkbox boutique_en_ligne "bar | Foo"]

    Both would output html like this:

    <label>
        <input type="checkbox" name="boutique_en_ligne[]" value="bar">
        Foo
    </label>

    And in the $_POST['boutique_en_ligne'] variable, that’d give the raw info like this:

    Array(1) (
        [0] = "bar"
    )

    Compared to $posted_data['boutique_en_ligne'] that gives the label info like this:

    Array(1) (
        [0] = "Foo"
    )

    I guess the other question is what does your form tag look like? Is that also being dynamically generated and prevents the options from being switched out?

    Thread Starter db03

    (@db03)

    Thank you for the responses!

    I have worked around by using $_POST variable instead of $posted_data. It was way simpler for me since I only needed the key to be stored in the database.

    I really appreciate your support

    Plugin Author Tessa (they/them), AuRise Creative

    (@tessawatkinsllc)

    Perfect!

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Help! Options key|value opposite of CF7’ is closed to new replies.