wordpress 如何在发送前挂钩联系表格 7

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29926252/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 11:05:22  来源:igfitidea点击:

How to hook into Contact Form 7 Before Send

wordpresswordpress-plugincontact-form-7

提问by Naterade

I have a plugin I am writing that I want to interact with Contact Form 7. In my plugin I added the following action add_action

我正在编写一个插件,我想与联系表 7 进行交互。在我的插件中,我添加了以下操作 add_action

add_action("wpcf7_before_send_mail", "wpcf7_do_something_else");

function wpcf7_do_something_else(&$wpcf7_data) {

    // Here is the variable where the data are stored!
    var_dump($wpcf7_data);

    // If you want to skip mailing the data, you can do it...
    $wpcf7_data->skip_mail = true;

}

I submitted the contact form but the add_action I had did nothing. I'm unsure how to make my plugin intercept or do something when Contact Form 7 does something. Any, help on how to do this?

我提交了联系表格,但我没有做任何 add_action。当 Contact Form 7 执行某些操作时,我不确定如何让我的插件拦截或执行某些操作。任何,有关如何执行此操作的帮助?

回答by Musk

I had to do this to prevent Email from being sent. Hope it helps.

我必须这样做以防止发送电子邮件。希望能帮助到你。

/*
    Prevent the email sending step for specific form
*/
add_action("wpcf7_before_send_mail", "wpcf7_do_something_else");  
function wpcf7_do_something_else($cf7) {
    // get the contact form object
    $wpcf = WPCF7_ContactForm::get_current();

    // if you wanna check the ID of the Form $wpcf->id

    if (/*Perform check here*/) {
        // If you want to skip mailing the data, you can do it...  
        $wpcf->skip_mail = true;    
    }

    return $wpcf;
}

This code assumes that you are running the latest version of CF7 your code above used to work until a couple months ago when they went and did some refactoring of the code. [Apr 28 '15]

这段代码假设您运行的是最新版本的 CF7,您的上面的代码过去一直可以工作,直到几个月前他们对代码进行了一些重构。[28 年 4 月 28 日]

回答by d79

I'd like to add that you could just use the wpcf7_skip_mailfilter:

我想补充一点,您可以只使用wpcf7_skip_mail过滤器:

add_filter( 'wpcf7_skip_mail', 'maybe_skip_mail' );

function maybe_skip_mail( $skip_mail, $contact_form ) {

    if( /* your condition */ )
        $skip_mail = true;

    return $skip_mail;

}, 10, 2 );

回答by Dan Canetti

You can turn Demo Mode on in additional settings and this will prevent emails from being sent. See below from the CF7 Docs.

您可以在其他设置中打开演示模式,这将阻止发送电子邮件。请参阅下面的 CF7 文档。

If you set demo_mode: onin the Additional Settings field, the contact form will be in the demo mode. In this mode, the contact form will skip the process of sending mail and just display “completed successfully” as a response message.

如果您demo_mode: on在“附加设置”字段中进行设置,则联系表单将处于演示模式。在这种模式下,联系表单将跳过发送邮件的过程,只显示“成功完成”作为响应消息。