( function( $ ) {

  $( document ).on( 'wpcf7submit', '.wpcf7', function ( e ) {

    if ( 'validation_failed' === e.detail.status ) {
      return;
    }

    window.location = 'https://example.com/thanks';

  } );

} )( jQuery );

How to use it?

Usually adding functionality to a WordPress site is by creating a plugin.
However you can simply copy the code and paste it in your child theme’s functions.php file at the end of the file, just before ?>.

For JavaScript code you will need to add them to your theme’s main JavaScript file.

Having issues with the code?

If the code doesn’t work or you get an error please let us know in the comments section.
We will do our best to fix it as-soon-as-possible and update the code on this page.

Comments

  1. Dave McHale

    Suggestion to update this to show how to redirect CONDITIONALLY based on which form was being submitted. Instead of a single window.location to the same destination for all forms, you can wrap it in a check for the ID of the form that was submitted.

    if ( 12345 === e.detail.contactFormId ) {
        window.location = 'https://example.com/thanks';
    }
    if ( 67890 === e.detail.contactFormId ) {
        window.location = 'https://example.com/thanks-2';
    }
    Reply
    1. Dor Zuberi

      Hi Dave,
      Thanks for the comment, you can do a lot of things with the data in the event object.

      The code snippet is the base, you can always add, remove and modify the code 🙂

    2. Sam

      Thank you Dave!

Leave a Reply