联系表格 7:使用 wpcf7_before_send_mail 创建的挂钩仅用于一个按 ID 的联系表格

Contact Form 7: use hook created using wpcf7_before_send_mail for only one contact form by id

我在一个网站上工作,该网站有多个使用 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。

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;

}

希望对您有所帮助。

我使用的是 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,但这行得通,我对结果很满意。

自 2015 年以来,此插件中检索表单 ID 和提交字段的方法发生了变化,again in 2020 自撰写此答案以来。

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

$form_id = $contact_form->id();

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

$posted_data = $submission->get_posted_data();

将所有内容放在一起,您的代码段将如下所示:

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

下面是我在此答案中写的原始片段,可用于未传递 $submission 变量的旧版本的 Contact Form 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 ) );
    }
}

我稍微改变了很好的答案是@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'] ) );

        }

    }
}