将wordpress操作重定向到functions.php


Redirect on a wordpress action into functions.php

我无法在functions.php中定义的wordpress 动作上实现重定向。动作是wpcf7_before_send_mail,这个函数在一个特定的条件下不能发送联系人表单电子邮件。

add_action( 'wpcf7_before_send_mail', 'my_function' ); function my_function($wpcf7_data) { //redirect to another page }
你能帮我一下吗?

如果我理解你的问题,你想中止发送邮件并重定向到一个页面?

我不太熟悉联系人表单7,但我认为你应该使用wpcf7_skip_mail而不是wpcf7_before_send_mail,然后使用wp_redirect()重定向到另一个页面。所以你的代码应该看起来像这样:

function skip_mail($skip_mail, $contact_form) {
    // Put in ur condition
    if ($condition) {
        // The redirect (look at the attached url)
        wp_redirect($url)
        // Return ture if you want to skip sending the mail.
        return true;        
    }
    // Return false if you want to send the mail.
    return false;   
}
add_filter('wpcf7_skip_mail', 'skip_mail', 10, 2);

我没有测试代码,但认为它应该工作。

Btw,这里是另一个使用wp_redirect与CF7的例子。

由于CF7在javascript中引入了一些事件,您可以尝试将此HTML添加到表单中:

<script>
    document.addEventListener( 'wpcf7mailsent', function( event ) {
        location = 'http://example.com/';
    }, false );
</script>