• Resolved netschmiede24

    (@netschmiede24)


    Hi,

    I am trying to achieve the following:

    I have a contact form with a select field. The options are something like this:

    [select* your-subject first_as_label "Please choose option" "General Requiry | [email protected]" "Support | [email protected]" "Project | [email protected]" "Technology| [email protected]"]

    So basically, the user selects a SUBJECT for the Contact form (General Requiry, Support, Project or Technology) – but in reality, the subject defines the recipient. So, I put [your-subject] in the recipients field and the form gets sent to the correct address. This works fine with the regular select field.

    Now on the website, I have for example a page “Support”. And there is a button “Get support”. I want this button to link to the page with the contact form, and have “Support” preselected.

    I tried the following for the dynamic select field:

    [dynamic_select* your-subject include_blank dtx_disable_blank "[email protected] | General requiry" "[email protected] | Support" "[email protected] | Project" "[email protected] | Technology"] </div>

    Now what I need is the correct link for the button. With

    https://www.domain.com/contact#form

    I will get directly to the form. But how to preselect the dynamic select field?

    I tried something like this:

    https://www.domain.com/contact/?your-subject=Support#form

    Unfortunately, that doesn’t work.

    Can you help?

    Thanks,

    Astrid

Viewing 6 replies - 1 through 6 (of 6 total)
  • Your URL should be: https://www.domain.com/contact#form[email protected]

    then you can read the emailaddress like this:

    [dynamic_select* your-subject default:"CF7_GET key='EmailAddress'" "[email protected] | General requiry" "[email protected] | Support" "[email protected] | Project" "[email protected] | Technology"]

    but you need to encode it to this:

    [dynamic_select* your-subject default:CF7_GET%20key%3D%26%2339%3BEmailAddress%26%2339%3B "[email protected] | General requiry" "[email protected] | Support" "[email protected] | Project" "[email protected] | Technology"]
    Thread Starter netschmiede24

    (@netschmiede24)

    Hi,

    unfortunately this doesn’t work.

    The link https://www.domain.com/contact#[email protected] will not scroll to the ID #form (but it does, if I put it this way: https://www.domain.com/contact/[email protected]#form) – and it will not preselect the desired subject.

    Also – I do not want to make the E-mail address public (as it would be explictely in the URL with your suggestion) – I just want the visitor to select the SUBJECT they are interested in and then turn it into the desired email address.

    And another point: With your configuration I cannot enter [your-subject] in the recipient field in the E-mail tab of CF7 at all – so the whole point of selecting a recipient is not working.

    Any other idea?

    Thanks,

    Astrid

    • This reply was modified 10 months, 3 weeks ago by netschmiede24.

    can you try like this, i’m quite sure it will select the correct value when you use the visible text: https://www.domain.com/contact?EmailAddress=Support#form

    you can also try to use the index of the select:

    https://www.domain.com/contact?EmailAddress=1#form

    https://www.domain.com/contact?EmailAddress=2#form

    https://www.domain.com/contact?EmailAddress=3#form

    [dynamic_select* your-subject default:CF7_GET%20key%3D%26%2339%3BEmailAddress%26%2339%3B "[email protected] | General requiry" "[email protected] | Support" "[email protected] | Project" "[email protected] | Technology"]

    the field has name: your-subject, so you should be able to use this in your recipient field.

    you can see a working example here: https://aurisecreative.com/docs/contact-form-7-dynamic-text-extension/shortcodes/dtx-shortcode-php-get-variables/?foo=bar&hello=world

    it will populate 2 field: foo and Hello, with text: bar and world.

    • This reply was modified 10 months, 3 weeks ago by h1lbert.
    • This reply was modified 10 months, 3 weeks ago by h1lbert.
    Thread Starter netschmiede24

    (@netschmiede24)

    No, the link https://www.domain.com/contact?EmailAddress=Support#form does not work either. It will not change the value of the Select field.

    Your example is with a dynamic TEXT field – not with a dynamic SELECT field.

    Also if I put [your-subject] into the recipients field I get an error saying: “Only email, dynamic email, hidden, or dynamic hidden form tags can be used for email addresses.” (Although the regular SELECT field CAN be used for recipients as well – just not the DYNAMIC SELECT).

    Thanks,
    Astrid

    Hi! I just wanted to pop-in and say I read this and will address it in September. My day-job and side hustle have kept me busy so I haven’t had the time to provide the free support and updates as I would like to. Cheers!

    Also, thank you @h1lbert for helping people! I appreciate your efforts 🙂

    Hi @netschmiede24 I apologize, your thread fell through the cracks a little, so thank you for your patience.

    It looks like your request has two goals:

    1. Pre-select the subject in a select field for the support page
    2. Determine the email recipient based on the subject without revealing the email address

    I like to work backwards for some reason, so I’m going to start with your second goal. For that, I have a blog article that covers this here. For your situation, your code snippet will look like this:

    /**
    * Custom Email Behavior on Contact Form 7 Form Submission
    *
    * @param WPCF7_ContactForm $contact_form The current contact form.
    * @param bool $abort If the submission is being aborted.
    * @param WPCF7_Submission $submission The current submission data.
    *
    * @return void
    */
    function netschmiede24_before_send_mail($contact_form, &$abort, $submission)
    {
    // Get the submitted value of the subject from your select field
    $subject = strtolower(trim(sanitize_text_field($_POST['your-subject'])));

    // Define the allowed subjects and their associated email addresses
    $allowed = array(
    'general requiry' => '[email protected]',
    'support' => '[email protected]',
    'project' => '[email protected]',
    'technology' => '[email protected]'
    );

    // Validate their choice, or choose the first one so an email address is always selected
    if(!array_key_exists($subject, $allowed)) {
    $subject = array_keys($allowed)[0];
    }

    // Get the email address
    $email = sanitize_email($allowed[$subject]);

    // Update recipient so they receive it instead of whatever was set
    $mail = $contact_form->prop('mail');
    $mail['recipient'] = $email;
    $contact_form->set_properties(array('mail' => $mail);
    }
    add_action('wpcf7_before_send_mail', 'netschmiede24_before_send_mail', 10, 3);

    Then in your email configuration, you can put whatever email address you want that passes validation, because it will be ignored and replaced by the email address set in the code snippet above. Since this happens after the form is submitted, the user will not see the recipient.

    To achieve your first goal, we can simplify your select field’s options to only the subjects since the email addresses are being sorted after. So in your mail template, you can use this:

    [dynamic_select* your-subject include_blank dtx_disable_blank "General Requiry" "Support" "Project" "Technology"]

    For your support page, we can do it two ways, we can set it as a query in the URL so when the button is clicked, it’s preselected, but that won’t preselect it if the user gets to the support page through another means. So instead, I’m going to use the page’s title. This would also work for pages named “Project”, “Technology” and “General Requiry”. To do this, we’re going to use the default attribute and it does have to be encoded like @h1lbert said in order to work. So now your select field tag will look like this:

    [dynamic_select* your-subject include_blank default:CF7_get_post_var%20key%3D%26%2339%3Bpost_title%26%2339%3B dtx_disable_blank "General Requiry" "Support" "Project" "Technology"]

    So now whenever someone opens that form on your Support page, regardless of how they arrived, it will pre-select the “Support” subject and when submitted, will send to your [email protected] address without revealing the address to the end user.

Viewing 6 replies - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.