【问题标题】:Contact Form 7: use hook created using wpcf7_before_send_mail for only one contact form by id联系表格 7:使用使用 wpcf7_before_send_mail 创建的挂钩仅用于 id 的一个联系表格
【发布时间】:2015-08-12 12:50:09
【问题描述】:

我正在使用 Contact Form 7 创建的多个表单的站点工作。对于其中一个表单,我正在传递我使用表单中的隐藏输入字段收集的变量。我正在使用 wpcf7_before_send_mail 挂钩将这些变量传递到电子邮件中,但是这些值正在传递到每封电子邮件中(我添加了动态变量和静态文本)这是代码:

add_action( 'wpcf7_before_send_mail', 'wpcf7_add_text_to_mail_body' );

 function wpcf7_add_text_to_mail_body($contact_form){
     $values_list = $_POST['valsitems'];
     $values_str = implode(", ", $values_list);

     // get mail property
     $mail = $contact_form->prop( 'mail' ); // returns array 

     // add content to email body
     $mail['body'] .= 'INDUSTRIES SELECTED';
     $mail['body'] .= $values_list;


     // set mail property with changed value(s)
     $contact_form->set_properties( array( 'mail' => $mail ) );

 }

我试图弄清楚如何仅将这些值传递给联系表单电子邮件模板之一,可能是通过表单 ID。

【问题讨论】:

    标签: php forms wordpress contact-form-7


    【解决方案1】:

    我稍微改变了最佳答案是@Tessa
    因为它对我不起作用。看来
    注入需要稍微不同的结构
    不同的电子邮件地址。

    希望这对任何人都有帮助。

    /********************************************************
    **  IF COUNTRY == USA && ON CONTACT US PAGE 
    **  USE A DIFFERENT RECIPIENT EMAIL
    ********************************************************/
    add_action( 'wpcf7_before_send_mail', 'wpcf7_add_text_to_mail_body', 10, 3 );
    function wpcf7_add_text_to_mail_body( $contact_form, $abort, $submission ) {
    
        //Get the form ID
        $form_id    = $contact_form->id();
        $new_email  = 'example@example.com';        // change email here
    
        // checking if this if a specific form by its ID
        if( $form_id == 572 ) {
    
            $posted_data    = $submission->get_posted_data();
    
            
            // i only changed the email is a select field named country was set to USA..
            if(!empty($posted_data['country'][0]) && $posted_data['country'][0] == 'United States') {
    
                // set mail property with changed value(s)
                $mailProp = $contact_form->get_properties('mail');
                $mailProp['mail']['recipient'] = $new_email;
                $contact_form->set_properties( array( 'mail' => $mailProp['mail'] ) );
    
            }
    
        }
    }
    

    【讨论】:

      【解决方案2】:

      自 2015 年以来,此插件中检索表单 ID 和提交字段的方法已更改,自撰写此答案以来,again in 2020 已更改。

      要获取表单 ID,您应该使用这个:

      $form_id = $contact_form->id();
      

      要获取提交数据,您应该使用它(而不是 $_POST)。函数get_posted_data() 返回一个字符串(如果您提供了一个专门用于拉取的键)或一个字符串值数组(如果没有发送参数并且您想要所有内容)。

      $posted_data = $submission->get_posted_data();
      

      总而言之,您的 sn-p 将如下所示:

      add_action( 'wpcf7_before_send_mail', 'wpcf7_add_text_to_mail_body', 10, 3 );
      function wpcf7_add_text_to_mail_body( $contact_form, $abort, $submission ) {
      
          //Get the form ID
          $form_id = $contact_form->id();
      
          //Do something specifically for form with the ID "123"
          if( $form_id == 123 ) {
              $posted_data = $submission->get_posted_data();
              $values_list = $posted_data['valsitems'];
              $values_str = implode(", ", $values_list);
      
              // get mail property
              $mail = $contact_form->prop( 'mail' ); // returns array 
      
              // add content to email body
              $mail['body'] .= 'INDUSTRIES SELECTED';
              $mail['body'] .= $values_list;
      
              // set mail property with changed value(s)
              $contact_form->set_properties( array( 'mail' => $mail ) );
          }
      }
      

      以下是我在此答案中编写的原始 sn-p,可用于未传递 $submission 变量的旧版联系表 7。

      add_action( 'wpcf7_before_send_mail', 'wpcf7_add_text_to_mail_body', 10, 1 );
      function wpcf7_add_text_to_mail_body( $contact_form ) {
      
          //Get the form ID
          $form_id = $contact_form->id();
      
          //Do something specifically for form with the ID "123"
          if( $form_id == 123 ) {
              $submission = WPCF7_Submission::get_instance();//Get the current form submission
              $posted_data = $submission->get_posted_data();
              $values_list = $posted_data['valsitems'];
              $values_str = implode(", ", $values_list);
      
              // get mail property
              $mail = $contact_form->prop( 'mail' ); // returns array 
      
              // add content to email body
              $mail['body'] .= 'INDUSTRIES SELECTED';
              $mail['body'] .= $values_list;
      
              // set mail property with changed value(s)
              $contact_form->set_properties( array( 'mail' => $mail ) );
          }
      }
      

      【讨论】:

      • 请提供look at my commentWPCF7_Submission 作为动作钩子的第三个参数提供。
      • @luukvhoudt 谢谢。在我在这里回答大约 7 个月后发布的联系表格 7 网站上的更新文档后,我更新了我的答案以包含第三个论点:)
      • 您没有为add_action 钩子提供第三个和第四个参数给add_action 函数。你至少应该将第四个参数设置为3,否则如果我没记错的话你会得到一个PHP警告。
      • 啊,是的,我忘了添加那部分。您是正确的,因为第 4 个参数的默认值是 1,我们肯定希望通过 CF7 更新将其更改为 3
      【解决方案3】:

      我正在使用 Dinesh 的答案,但它对我不起作用。相反,我现在正在检查我提交的表单所独有的字段:

      add_action( 'wpcf7_before_send_mail', 'wpcf7_add_text_to_mail_body' );
      function wpcf7_add_text_to_mail_body($contact_form){
      
         $submission = WPCF7_Submission::get_instance();
         $posted_data = $submission->get_posted_data();
         if( !empty($posted_data["dealer_email"])){  //use a field unique to your form
      
             $email = trim($posted_data["dealer_email"]);
             // more custom stuff here
         }
      }
      

      确保在您的每个表单中至少有一个唯一的表单名称,您可以使用它来执行此操作。仍然可以通过函数从 $contact_form 获取表单 ID,但这很有效,我对结果很满意。

      【讨论】:

        【解决方案4】:

        Contact Form 7 使用隐藏输入类型来存储表单 ID。它使用隐藏字段名称 _wpcf7。您可以通过这种方式获取表单 ID。

        $form_id = $contact_form->posted_data['_wpcf7'];
        

        所以你的最终代码应该是

        add_action( 'wpcf7_before_send_mail', 'wpcf7_add_text_to_mail_body' );
        
        function wpcf7_add_text_to_mail_body($contact_form){
         $form_id = $contact_form->posted_data['_wpcf7'];
         if ($form_id == 123): // 123 => Your Form ID.
             $values_list = $_POST['valsitems'];
             $values_str = implode(", ", $values_list);
        
             // get mail property
             $mail = $contact_form->prop( 'mail' ); // returns array 
        
             // add content to email body
             $mail['body'] .= 'INDUSTRIES SELECTED';
             $mail['body'] .= $values_list;
        
        
             // set mail property with changed value(s)
             $contact_form->set_properties( array( 'mail' => $mail ) );
         endif;
        
        }
        

        希望这会有所帮助。

        【讨论】:

        • 使用$contact_form->id()获取表单id
        • 这行不通,因为$posted_data 不是WPCF7_ContactForm 的属性(还有吗?)。使用提供给操作挂钩的第三个参数来访问“发布的数据”,其中包含类WPCF7_Submission。此类包含方法 get_posted_data($name = null) 您可以改用它。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-01-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-02-13
        相关资源
        最近更新 更多