• Resolved jglidewell

    (@jglidewell)


    I have the following dynamic select dropdown (has more values but excluded them for obvious reasons), I want to be able to use the value instead of the text that is shown. When I use the [staff] shortcode in the mail tab it is returning the text not the value (in this case would be the email address). Can anyone help with this?

    [dynamic_select staff id:staff class:form-control “[email protected]|Test”]

    Additional Headers section:
    Cc:[staff]

    • This topic was modified 12 months ago by jglidewell.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Absolutely! This was something I noticed the other day when I wanted to save values in the database and not the label! So if I’m understanding you right, you just want the shortcode in the mail template to use the value instead of the label. In that case, you’ll want to use the wpcf7_before_send_mail hook like this:

    /**
     * Custom CF7 Staff Email
     *
     * @param WPCF7_ContactForm $form The current contact form being processed.
     *
     * @returns void
     */
    function jglidewell_custom_staff_email($form)
    {
        // Check if this form is submitting the "staff" field
        if (array_key_exists('staff', $_POST)) {
            // Sanitize & validate staff value
            $staff = trim(sanitize_email($_POST['staff']));
            if (!empty($staff)) {
                $mail = $form->prop('mail'); // Get the first mail property
                if ($mail['active']) {
                    // If the first email is active, replace the shortcode with the value
                    $mail['additional_headers'] = str_replace('[staff]', esc_html($staff), $mail['additional_headers']);
                }
                $mail2 = $form->prop('mail_2'); // Get the second mail property
                if ($mail2['active']) {
                    // If the second email is active, replace the shortcode with the value
                    $mail2['additional_headers'] = str_replace('[staff]', esc_html($staff), $mail2['additional_headers']);
                }
                // Update the form with the updated values
                $form->set_properties(array(
                    'mail' => $mail,
                    'mail_2' => $mail2
                ));
            }
        }
    }
    add_action('wpcf7_before_send_mail', 'jglidewell_custom_staff_email');
    
    

    See the Contact Form 7 documentation regarding accessing user input data for more info about the difference between using $_POST and the posted data returned from $submission->get_posted_data().

    Cheers!

    Thread Starter jglidewell

    (@jglidewell)

    Yes! This is perfect, thank you.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘How to get Dynamic Select Value to Use as a CC’ is closed to new replies.