Contact Form 7 在发送电子邮件后不重定向

Contact Form 7 not redirecting upon email sent

我有一个表单,我试图在成功发送电子邮件后将其重定向到 http://www.example.com。我尝试了不同的方法,包括在附加设置中 on_sent_ok 以及

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

在我的 JavaScript 中,但这似乎并不奏效。

编辑:我忘了提到,在用户点击提交时,我做了一个防止默认的操作,以便进行一些计算并生成 PDF。一旦完成,我就做

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

允许提交。这会导致重定向出现任何问题吗?

Contact Form 7 打了一个 ajax 电话。成功后元素被插入。然后你可以检查元素是否存在:

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

好吧,也许我写晚了,但这段代码肯定能完成这项工作。 (如果你在 wordpress 工作)。我目前正在使用它并且它工作正常。

请记住将此代码放在您的函数文件中,最后请记住,您必须使用其中之一,而不是两者...!

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>";

    }
}

引用here