【问题标题】:How to hook into Contact Form 7 Before Send如何在发送前连接到联系表 7
【发布时间】:2015-07-07 16:58:13
【问题描述】:

我正在编写一个插件,我想与 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 时如何让我的插件拦截或做某事 做某事。有什么帮助吗?

【问题讨论】:

    标签: wordpress contact-form-7


    【解决方案1】:

    我必须这样做以防止电子邮件被发送。希望对您有所帮助。

    /*
        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,您的上述代码一直可以正常工作,直到几个月前他们对代码进行了一些重构。 [2015 年 4 月 28 日]

    【讨论】:

    • 感谢马斯克,做得很完美!这也回答了我关于如何操作另一个插件的问题!
    • 请确保不要在您的函数中使用 echovar_dump 任何内容。
    • @Musk - 问题:我会在我的functions.php中添加这段代码吗?另外,我如何能够“回显”或其他东西来验证它在发送之前是否正在运行?谢谢!
    • @Radmation 是的,如果你回显它会出现在 Chrome 的网络选项卡中,很可能在 admin-ajax.php 中,来验证函数中的某些内容。
    • 好像4.8以后就不行了 开发者官方没有说明原因。相关:wordpress.org/support/topic/…我不得不降级到 4.7 版
    【解决方案2】:

    我想补充一点,您可以使用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;
    
    }
    

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        【解决方案4】:

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

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

        【讨论】:

          猜你喜欢
          • 2017-04-24
          • 2015-12-23
          • 2021-08-19
          • 1970-01-01
          • 2018-04-16
          • 2018-04-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多