自2015年以来,此插件中检索表单ID和提交字段的方法已更改,自编写此答案以来,在2020年再次更改。
要获取表单 ID,您应该使用以下命令:
$form_id = $contact_form->id();
若要获取提交数据,应使用此选项(而不是 $_POST)。该函数返回一个字符串(如果您提供了专门用于拉取的键)或一个字符串值数组(如果未发送任何参数并且您需要所有内容)。get_posted_data()
$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 ) );
}
}
以下是我在此答案中编写的原始代码段,可用于未传递该变量的旧版本的联系表单7。$submission
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 ) );
}
}