Search code examples
javascripthtmlwordpresscontact-form-7

How to prevent redirect to a certain URL after submission of Contact Form 7 without input required fields


My goal is to redirect to an external URL after submission of the Contact Form 7 but only if all required fields are filled in. Instead of this I get a redirect every time, even if all fields are blank. Fields without input are being marked as invalid but it is not preventing redirection.

<input type="submit" value="Send" class="wpcf7-form-control wpcf7-submit" onclick="window.location.href='https://example.com'" />


Solution

  • There are some events which will help you to achieve your scenario. In your case event should be called once email sent so you can use wpcf7mailsent for this.

    At the footer add following code.

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