Contact Form 7 Redirect Multiple Forms To Different Pages After Submission Without A Plugin

Updated March 5th, 2019
Updated March 5th, 2019
Share this post:
Share on facebook
Share on twitter
Share on reddit
Share on linkedin
Share on pinterest
Share on pocket
Share on email
Share on print

Use the script on this page to redirect visitors to specific pages after they have submitted a form on your site. Using the second script on this page you can redirect more than one form and each to different pages.

Paste the code below into your functions.php file. Change the form IDs, in the example below they are 936 and 937, to match the IDs of your forms and change the “location” URLs to the URLs you desire.

I walk you through how to do it in this video:

More Contact Form 7 Tutorials you may want to check out after this one:

If you want to add more just copy and paste the else if statement.

Redirecting one form

add_action( 'wp_footer', 'cf7_thank_you_redirect' );
 
function cf7_thank_you_redirect() {
?>


	

Responses

Your email address will not be published. Required fields are marked *

  1. Hello B, I need to implement this function but I have an individual contact form on over 20 pages, is this the right thing to do? ( also, the CF7 success redirects plugin seems to no longer work? )

    1. I believe this is the thing to do because setting up another form submission/thank page redirect relationship is as easy adding a couple lines of code. Will all of the individual contact forms be redirected to the same thank you page or does each have its own unique thank you page?

      I’m not sure about the CF7 success redirects plugin no longer working, but with the update to 4.9 a bunch of plugins stopped working.

  2. Hello Bjorn, thanks for the tip. I added your code to my function.php file and redirect is working good. However the tiny loading gif that appears nex to the Submit button and is no more being displayed as well as the the feedback message at the bottom. Do you think it can be related to the snippet injection?

    1. Hi Luca,

      If you remove the code from your functions.php and the loading gif and the feedback message come back then it is probably related to the code. I’ll have to look into it. Which browser were you using when you saw that they were missing?

  3. Hi Bjorn

    Thanks for the code. This is exactly what we wanted. I have installed the code in my themes function.php file, However, the redirection happened only once. Now the form still shows the old message and does not get redirected. I have tried clearing the cache.

    Let me know if there is an alternate solution to this problem.

    1. Hi Kunal,

      I’m not sure why it would work only once. Have you tried in different browsers or maybe on your phone? This code should redirect after every submission.

  4. Your videos are great and I am now following you.

    I’m an novice at coding and I’ve only been working with the WordPress CMS for a few years.

    I can find plugins to allow what I need to accomplish, but I should not have to purchase one for this simple function. Plus if what happens again that happened recently regarding plugins not working due to upgrades, I’d rather not purchase. I am close using multiple code sources, but to no avail.

    What I would like to do is use similar code as above, but grab a calculated sum on a single form and then send this to PayPal to process payment.

    Please advise.

  5. Hi Bjorn, when I try to insert the following code:

    add_action( ‘wp_footer’, ‘cf7_thank_you_redirect’ );

    function cf7_thank_you_redirect() {
    ?>

    <?php
    }

    I get the following error message:

    "Your PHP code changes were rolled back due to an error on line 743 of file wp-content/themes/TheFox/functions.php. Please fix and try saving again.

    syntax error, unexpected '}'"

    What is going wrong here?

  6. Whoops:

    add_action( ‘wp_footer’, ‘cf7_thank_you_redirect’ );

    function cf7_thank_you_redirect() {
    ?>

    <?php
    }

  7. Well, it’s not showing, but I basically replaced the number values and the thank you page URLs. Is there anything else I need to do or is it just not compatible with my theme?

  8. Nevermind, I figured it out. There was a small “?>” that I had to put the code inside of. I’m not a coder but I figured it out! Sorry for spamming you!

    1. No worries Mark, I’m glad you figured it out!

      Great-than and less-than brackets being changed happens quite a lot when copying/pasting code into different places.

  9. Help me?
    I have a custom template… only a index page, any codes works for me..
    what i have to do?
    I’m using the last version of wordpress en CF7

  10. Hi Bjorn,

    How can I redirect single form to multiple url’s based upon the drop down selection or radio buttons?

    1. Hi Krishna,

      This is a little bit tricky. You’ll have to use the code from this post for your Javascript.

      Combine that with what is on the CF7 help page here: https://contactform7.com/dom-events/

      This is the code you need from the help page:

      document.addEventListener( ‘wpcf7submit’, function( event ) {
      var inputs = event.detail.inputs;

      for ( var i = 0; i < inputs.length; i++ ) {
      if ( ‘your-name’ == inputs[i].name ) {
      alert( inputs[i].value );
      break;
      }
      }
      }, false );

      In that sample code "your-name" is the name given to your input field inside the CF7 builder. Change it to the name of your conditional field. Then use the If statement to determine if the input matches option A or option B. Then redirect in the If statement.

      Does that make sense? This may be a good tutorial video.

  11. Hello Sir,
    it worked very well but i want to open url in new tab.

    1. Hi Neeta,

      You should be able to change the “location” portion of the code to use “window.open(url, ‘_blank’);”. For example, this should open the URL in a new tab:

      I haven’t tested it yet, but it should work…I hope that helps!

  12. hello sir, why we choose “wp_footer” as a hook to execute it…please can u tell reason behind it…

    1. Hi Hardik,

      We use wp_footer as the hook to make sure that the form is fully loaded on the page. You could probably use wp_head though and it may work most of the time.

  13. Hi Bjorn,

    I have added in the code to the bottom of my functions PHP file, replace the ID and URL with the correct one, but all i get when the form is submitted is a continual processing wheel. it never rejects the form or redirects? Any ideas, am i supposed to be replacing a part of the code rather than just adding in your code?

    1. Hi Matthew,

      It sounds like you’ve done it correctly. The only things to change are the IDs and the URLs. Have you made sure that all your plugins and theme and WP core files are up-to-date? There could be a conflict somewhere.

  14. Hi Bjorn
    I am learning a lot good things from your videos. Thanks for that.
    Can i change the function (‘wpcf7mailsent’), say for example
    I have created drop down list and it contains 2 items.
    So if anyone selects
    Item 1 it should redirect to Page 1
    (OR)
    if anyone selects Item 2 it should redirect to Page 2
    is there any solution for this if yes then please help.
    Thanks in advance
    Love you

    1. Hi Rajeev,

      I’m glad I’m helping you on your WordPress journey 🙂 Thanks for being part of the WPLearningLab community!

      What you are looking for is possible. You can do almost anything with JavaScript and it’s libraries (like jQuery).

      In this case you would still need use wpcf7mailsent so that redirect happens after someone submits the form. You could make it so immediately after a dropdown choice is selected they are redirected, but then no email would be sent.

      document.addEventListener( ‘wpcf7mailsent’, function( event ) {
      var inputs = event.detail.inputs;

      if ( ‘input-name’ == ‘name-of-item-1’ ) {
      location = ‘http://url-1’; } else {
      location = ‘http://url-2’;
      }
      }, false );

      You’ll have to replace “input-name” with the name of the select field in your CF7 builder. And the “name-of-item-1” will need to be replaced with the name of the dropdown item.

      The code will redirect to url-1 if item one is chosen. If anything besides item one is chosen then it will redirect to the url-2.

      I haven’t tested this code yet, so it may need some tweaking.

      Does that make sense?

  15. Hi Bjorn,

    I tried both the codes as per your you tube instructions, but its not working for me. Can you please tell me what would be the reasons.

    1. Are you getting any errors? Or do you have any other information that would help me troubleshoot? Even better, join the Facebook group and paste your form code and JS code so that I can see what you’re trying.

  16. Hi Bjorn,
    I am using “arforms ” and creating a form which includes only dropdowns. This form has dropdowns of state, city, district and villages.
    My first question is can I redirect one submit button to different pages? For example, if I select a state: Telangana, District: Hyderabad, Village: Ameerpet. So when I click submit it should go to the page Ameerpet and again when I change village as Banjara hills it should go to the page bunjara hills.
    2nd question: Is it possible with the code that you have given here?
    3rd question: Will this work for “arform”?
    If you have a solution for this, then please let me know.

    Thank you

    1. Hi Vijay,

      What you’re asking is possible, but not with this exact code. You can use this code as a starting point though.

      This is code is specific to contact form 7 because it uses “wpcf7mailsent” as an event to listen for. That’s a cf7-only event. If arforms has DOM events the script can “listen” for then you could just plug it in.

      Once you find a way for Javascript to detect form selections then you can redirect each village to specific pages using Javascript IF statements. This will work even for arforms.

    1. Hi Suzy,

      It’s always best to use a child theme when you are making changes to the functions.php file. If you already have one created you don’t have to make a new one. You can edit the functions.php file in your existing child theme.

      I hope that helps and thanks for stopping by!

WPLearningLab