How to change contact form 7 Redirecting URL dynamically - WordPress
Solution 1
I have found a way to change the redirection URL dynamically. I have followed the following steps to achieve the dynamic redirection:
-
In contact form 7's Additional setting put the following:
on_sent_ok: 'redirect();'
-
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"
Create a template page or exiting page template ok.
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.
open your template file in an Editor.
-
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>
-
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;
}
}
Anam
Updated on June 09, 2020Comments
-
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/';"
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 over 9 yearsThank 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 over 6 yearsWhere as this is true, this doesn't answer the concept of redirecting to a dynamic URL
-
icc97 over 6 years
on_sent_ok
is going to be removed in 2017 -
icc97 over 6 yearsFrom the CR7 FAQ on redirects
on_sent_ok
is going to be removed in 2017, but theredirect
function should still apply to the newwpcf7mailsent
event mentioned in the same FAQ -
Ashar Zafar over 6 yearsyou 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 over 6 yearsIf 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 over 6 yearsFrom 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 over 6 yearsIt will work if you turn off the CF7 JavaScript