简体   繁体   English

将wordpress动作重定向到functions.php

[英]Redirect on a wordpress action into functions.php

I can not realize a redirect on a wordpress action defined in functions.php . 我无法实现对functions.php中定义的wordpress 操作重定向 The action is the wpcf7_before_send_mail , this function in one specific condition must not send the contact form email. 动作是wpcf7_before_send_mail ,此功能在特定条件下一定不能发送联系表电子邮件。

add_action( 'wpcf7_before_send_mail', 'my_function' ); function my_function($wpcf7_data) { //redirect to another page }

Can you help me please? 你能帮我吗? I am not very prepared on wordpress customizzation. 我对wordpress customizzation不太准备。

If I understand your question you want to abort the sending of a mail and redirect to a page? 如果我理解您的问题,您想中止邮件发送并重定向到页面吗?

I'm not super familiar with Contact Form 7, but I think you should use wpcf7_skip_mail instead of wpcf7_before_send_mail, and then use wp_redirect() to redirect to another page. 我对联系表7不太熟悉,但是我认为您应该使用wpcf7_skip_mail而不是wpcf7_before_send_mail,然后使用wp_redirect()重定向到另一个页面。 So your code should look something like this: 因此,您的代码应如下所示:

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

I havn't tested the code, but think it should work. 我没有测试过代码,但认为它应该可以工作。

Btw, here is another example on using wp_redirect with CF7. 顺便说一句,这是将wp_redirect与CF7一起使用的另一个示例

Since CF7 introduced some event in javascript you can try add this HTML into form: 由于CF7在javascript中引入了一些事件,因此您可以尝试将此HTML添加到表单中:

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

In CF7 page you can find that. CF7页面中,您可以找到它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM