wordpress 如何在发送前连接到联系表7

r6vfmomb  于 2023-03-17  发布在  WordPress
关注(0)|答案(5)|浏览(136)

我正在编写一个插件,希望与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;

}

我提交了联系表单,但add_action什么也没做。我不知道如何让我的插件拦截或做一些事情时,联系表单7做的东西。任何,帮助如何做到这一点?

jxct1oxe

jxct1oxe1#

我不得不这样做,以防止电子邮件被发送。希望它有帮助。

/*
    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;
}

这段代码假定您运行的是CF7的最新版本,您的上述代码在几个月前还能正常工作,当时他们对代码进行了一些重构。[Apr 28 '15]

snvhrwxg

snvhrwxg2#

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

或者将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 );

获取用户输入的数据

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

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

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

pdtvr36n3#

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

add_filter( 'wpcf7_skip_mail', 'maybe_skip_mail', 10, 2 );

function maybe_skip_mail( $skip_mail, $contact_form ) {

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

    return $skip_mail;

}
gcuhipw9

gcuhipw94#

您可以在其他设置中打开演示模式,这将阻止发送电子邮件。请参阅下面的CF7文档。
如果您在“附加设置”字段中设置了demo_mode: on,联系人表单将处于演示模式。在此模式下,联系人表单将跳过发送邮件的过程,只显示“已成功完成”作为响应消息。

bvjveswy

bvjveswy5#

如果你想阻止电子邮件被发送,使用此代码。此代码将工作,如果你使用的是最新版本的WPCF7

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

  $form_id = $contact_form->id();   
  // Compare the form Id so other form will work as it is.
  if( $form_id == 123 ) {
       // Get field data. For example message field
       $message = $submission->get_posted_data('message');
       $message = trim($message);
       // If messsage field value is empty then set abort to true which will not sent email
       if( empty( $message ) ) {
           $abort = true;
       }
  }
}
add_action( 'wpcf7_before_send_mail', 'wpcf7_before_send_mail_function', 10, 3 );

相关问题