How to change contact form 7 Redirecting URL dynamically - WordPress

21,443

Solution 1

I have found a way to change the redirection URL dynamically. I have followed the following steps to achieve the dynamic redirection:

  1. In contact form 7's Additional setting put the following:

    on_sent_ok: 'redirect();'

  2. We need a hidden field to carry a piece of necessary information. However, Contact form 7 by default do not allow us to create hidden fields. The developer SevenSpark has developed an extension to allow hidden fields in Contact form 7. http://wordpress.org/extend/plugins/contact-form-7-dynamic-text-extension/ please download the plugin and install. you will see two new tags have been generated for the contact form 7. this will allow you to grab the value from $_GET variable. please check the details on plugin page.

    ex. http://example.com/?foo="bar"

  3. Create a template page or exiting page template ok.

  4. assign the template to the appropriate page. if you want to use the default template you do not need to create or assign any template.

  5. open your template file in an Editor.

  6. paste the following code:

    <script>    
    
        function redirect() {
    
    
    
            // your hidden field ID
               var filename = document.getElementById('redFilename').value;
    
            var url ='';
    
            //alert(document.getElementById('redFilename').value);
            if (filename == 'apple')
            {
    
    
                url= 'http://example.com/thankyou/';
    
    
            }
            else if (filename == 'orange')
            {
             url= 'http://example.com/thankyou_orange/';
            }    
    
    
     window.location = url;
    
            }
            </script>
    
  7. Now browse the link with GET parameter.

    ex. http://example.com/?redFilename="apple"

the hidden field of the contact form 7 will capture the redFilename value. if form submission is successful, it will redirect to the http://example.com/thankyou_orange/ page

Enjoy!!!!

Solution 2

add_action('wpcf7_mail_sent', 'ip_wpcf7_mail_sent');
function ip_wpcf7_mail_sent($wpcf7)
{
    $on_sent_ok = $wpcf7->additional_setting('ip_on_sent_ok', false);

    if (is_array($on_sent_ok) && count($on_sent_ok) > 0)
    {
        wp_redirect(trim($on_sent_ok[0]));
        exit;
    }
}
Share:
21,443
Anam
Author by

Anam

Updated on June 09, 2020

Comments

  • Anam
    Anam almost 4 years

    I am building a website for one of my clients and they want a function into their website some thing like the following:

    when people click the download link, a form will appear ( contact form 7) and after visitors put their details , it will re-directed to the download link.

    I able to re-direct to a new page after form submission by using following additional setting to the contact form 7.

    on_sent_ok: "location = 'http://example.com/';"
    

    enter image description here

    However, They have 10 files , I need to change the re-direction link 10 times to trigger the download for the appropriate file. I can do it by using 10 contact forms which will be very dirty.

    Is there any way i can change the re-direction URL dynamically?

    For example,

    http://example.com/?id=1
    http://example.com/?id=2
    
    <?php
    
    $id = $_GET['id'];
    
    $url= "http://example.com/id=?". $id; 
    
    
    ?>
    

    is there any way to change the following Location with $url ?

    on_sent_ok: "location = 'http://example.com/';"
    
  • Gopalakrishna Palem
    Gopalakrishna Palem over 9 years
    Thank you for sharing this - I am also looking for similar thing. But one question: what is stopping the user from directly entering the "final destination" url? For example, example.com/thankyou_orange - How to prohibit it and make the user fill the contact form mandatory?
  • icc97
    icc97 over 6 years
    Where as this is true, this doesn't answer the concept of redirecting to a dynamic URL
  • icc97
    icc97 over 6 years
  • icc97
    icc97 over 6 years
    From the CR7 FAQ on redirects on_sent_ok is going to be removed in 2017, but the redirect function should still apply to the new wpcf7mailsent event mentioned in the same FAQ
  • Ashar Zafar
    Ashar Zafar over 6 years
    you need to update your contact form 7 plugin to use this script as i mention above i try it on v7.4.9 and then place a contact form short code in any page and place this JS script any where on page and change url in JS where need to redirect your dynamic page after submission
  • icc97
    icc97 over 6 years
    If you read the question, the person asking can already do this, but they explicitly don't want to have to change the form for every page. They want to use part of the GET request or perhaps the variables submitted in the form.
  • icc97
    icc97 over 6 years
    From what I can tell from my attempts, this doesn't work because the CF7 form submits via AJAX, and so this just redirects that AJAX request. That means that the AJAX never returns a response and the CF7 form updating spinner never stops
  • icc97
    icc97 over 6 years
    It will work if you turn off the CF7 JavaScript