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

Nat*_*ade 11 wordpress wordpress-plugin contact-form-7

我有一个插件,我写的是我想与Contact Form 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;

}
Run Code Online (Sandbox Code Playgroud)

我提交了联系表单,但add_action我没有做任何事情.我不确定如何使用插件拦截或在联系表单7执行某些操作时执行某些操作.任何,帮助如何做到这一点?

Mus*_*usk 21

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

/*
    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;
}
Run Code Online (Sandbox Code Playgroud)

此代码假定您运行上述代码的最新版本的CF7,直到几个月前他们去了并对代码进行了一些重构.

  • 它似乎在版本4.8之后不起作用.开发人员没有关于原因的正式声明.相关:https://wordpress.org/support/topic/upgrade-to-cf-4-8-seems-to-have-stopped-my-functions-php-to-fire/我不得不降级到4.7版本 (2认同)

d79*_*d79 9

我想补充一点,你可以使用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 );
Run Code Online (Sandbox Code Playgroud)


luk*_*ger 5

自 WPCF7 5.2 以来,wpcf7_before_send_mail钩子发生了很大变化。作为参考,这里是如何在 5.2+ 中使用此钩子

跳过邮件

function my_skip_mail() {
    return true; // true skips sending the email
}
add_filter('wpcf7_skip_mail','my_skip_mail');
Run Code Online (Sandbox Code Playgroud)

或者添加skip_mail到管理区域表单上的“其他设置”选项卡。

获取表单 ID 或帖子 ID

function wpcf7_before_send_mail_function( $contact_form, $abort, $submission ) {

    $post_id = $submission->get_meta('container_post_id');
    $form_id = $contact_form->id();

    // do something       

    return $contact_form;
    
}
add_filter( 'wpcf7_before_send_mail', 'wpcf7_before_send_mail_function', 10, 3 );
Run Code Online (Sandbox Code Playgroud)

获取用户输入的数据

function wpcf7_before_send_mail_function( $contact_form, $abort, $submission ) {

    $your_name = $submission->get_posted_data('your-field-name');

    // do something       

    return $contact_form;
    
}
add_filter( 'wpcf7_before_send_mail', 'wpcf7_before_send_mail_function', 10, 3 );
Run Code Online (Sandbox Code Playgroud)

将电子邮件发送给动态收件人

function wpcf7_before_send_mail_function( $contact_form, $abort, $submission ) {

    $dynamic_email = 'email@email.com'; // get your email address...

    $properties = $contact_form->get_properties();
    $properties['mail']['recipient'] = $dynamic_email;
    $contact_form->set_properties($properties);

    return $contact_form;
    
}
add_filter( 'wpcf7_before_send_mail', 'wpcf7_before_send_mail_function', 10, 3 );
Run Code Online (Sandbox Code Playgroud)