Search
Close this search box.

Contact Form 7 Redirect After Submission Without A Plugin

[updated_on]
Share this post:

Place this code inside the header.php file of your child theme. For best results find the following function in your header.php file and place the code right before it:

wp_head();

Make sure you include the “script” tags otherwise it won’t work.

<script type="text/javascript">
document.addEventListener( 'wpcf7mailsent', function( event ) {
    location = 'http://google.com/';
}, false );
</script>

If you want to redirect multiple forms to different pages check out this tutorial. You can also redirect people based on what they selected in your form.

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

Share this post:

12 thoughts on “Contact Form 7 Redirect After Submission Without A Plugin”

  1. I saw the video and i really appreciate your efforts to make this kind of tutorial. Kindly share more wordpress tutorial like this which will help to grow more. Again thank you so much for this tutorial.

  2. What if I only want to add this function to a specific form on a specific page? I have other forms that I do not want this function on

  3. I’m using this script to give a .pdf e-book when the user completes the form. Is there a way to direct the URL to another tab or as a downloadable file?

    1. Hi Wayne,

      Instead of having a webpage URL in the Javascript you can put a link to the PDF. In some browsers, it will open that PDF in a new tab and in other browsers, it will download the PDF instantly after successful form submission.

      I hope that helps and thanks for stopping by!

  4. Hi.
    I’m trying to intergrate a courier service api into a checkout page, so that whenever the customer buys a product, there is an option for them to have it delivered to their door step.
    using wordpress of course, how do i make that happen or what steps should i take?
    Please help..
    Thanks.

    1. I assume you’re using WooCommerce. If you are, the easiest (and smartest) thing to do is look in the WooCommerce marketplace to see if they have a plugin for that courier already. If they don’t then you’ll have to go through the courier’s API documentation. If they have an API they must have sample code helping you integrate it into your checkout process.

      I hope that helps and thanks for stopping by!

  5. Hi Bjorn,

    How to redirect submission based on conditional field? For example a dropdown has option A and B, if user choose A they will redirect to URL_A after submit, if choose B redirect to URL_B after submit.
    Your video is very helpful. Thank you

    1. Hi Rizky,

      This is a little bit tricky. You’ll have to use the code from this post (https://wplearninglab.com/contact-form-7-redirect-multiple-forms/) 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 &lt; inputs.length; i++ ) {
      if ( 'your-name' == inputs[i].name ) {
      alert( inputs[i].value );
      break;
      }
      }
      }, false );

      In that sample code &quot;your-name&quot; 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.

  6. Thank you for the video.

    When I paste the code in the header.php as you have shown in the video I get this error message:

    Your PHP code changes were rolled back due to an error on line 59 of file wp-content/themes/Vela-child/header.php. Please fix and try saving again.
    syntax error, unexpected ‘<'

    I'm not sure what I'm doing wrong.

    Thanks.

    1. Hi Juliet,

      The specific error is that a less-than-greater-than sign (pointy bracket) “<" was in the wrong place. My feeling is that you tried to paste the code inside a

Leave a Comment

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

WPLearningLab