mikepsb
mikepsb

Reputation: 17

Contact Form 7 not redirecting upon email sent

I have a form that I am trying to have redirect to http://www.example.com upon successfully sending an email. I have tried different approaches including on_sent_ok in the additional settings as well as

if(jQuery('.wpcf7-mail-sent-ok').length > 0)   
window.location.replace("http://stackoverflow.com");

in my JavaScript, but that does not seem to work as well.

Edit: I forgot to mention that upon the user clicking submit, I do a prevent default in order to do some calculations and generate a PDF. Once it is all done I do

$("form.wpcf7-form").unbind('submit').submit(); 

to allow the submission to happen. Could this be causing any issues with the redirection?

Upvotes: 1

Views: 4445

Answers (2)

KAZZABE
KAZZABE

Reputation: 207

Well, maybe I'm writing late, but this code will definitelly will do the job. (If you're working in wordpress). I'm using it so far and it's working normally.

Remember to place this code at your functions's file and as final note remember that you must use one or the other, not both...!

add_action('wp_head', 'RedirectsCF7');
// Start of function.
function RedirectsCF7() {

    if(is_page("contact-page-or-whatever-page-name-is")) {

    echo "<script>document.addEventListener('wpcf7mailsent', function(event) {location = 'https://www.google.com/';}, false);</script>";

    }
}

// Or simply add this code to all pages, like this.
    if(!is_admin()) {

    echo "<script>document.addEventListener('wpcf7mailsent', function(event) {location = 'https://www.google.com/';}, false);</script>";

    }
}

Reference here

Upvotes: 0

FutureMode
FutureMode

Reputation: 113

Contact Form 7 made a ajax call. After success the element is inserted. Then you can check if element exist:

jQuery(document).ajaxComplete(function() {
  if (jQuery('.wpcf7-mail-sent-ok').length) {
    alert(1);
   //window.location.replace("http://stackoverflow.com");
  }
});

Upvotes: 4

Related Questions