ard*_*dev 10 php forms wordpress wordpress-plugin contact-form-7
我正在使用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 ) );
}
Run Code Online (Sandbox Code Playgroud)
我试图弄清楚如何只将这些值传递给联系表单电子邮件模板之一,可能通过表单ID.
小智 14
联系表单7使用隐藏的输入类型来存储表单ID.它使用隐藏字段名称_wpcf7.您可以像这样获取表单ID.
$form_id = $contact_form->posted_data['_wpcf7'];
Run Code Online (Sandbox Code Playgroud)
所以你最终的代码应该是
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;
}
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助.
Tes*_*ssa 14
自 2015年以来,此插件中检索表单 ID 和提交字段的方法发生了变化,自撰写此答案以来,2020 年又发生了变化。
要获取表单 ID,您应该使用:
$form_id = $contact_form->id();
Run Code Online (Sandbox Code Playgroud)
要获取提交数据,您应该使用它(而不是 $_POST)。该函数get_posted_data()返回一个字符串(如果您提供了一个专门用于拉取的键)或一个字符串值数组(如果没有发送参数并且您想要所有内容)。
$posted_data = $submission->get_posted_data();
Run Code Online (Sandbox Code Playgroud)
总而言之,您的代码段将如下所示:
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 ) );
}
}
Run Code Online (Sandbox Code Playgroud)
下面是我在这个答案中写的原始片段,可用于不传递$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 ) );
}
}
Run Code Online (Sandbox Code Playgroud)
我一直在使用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
}
}
Run Code Online (Sandbox Code Playgroud)
确保在每个表单中至少有一个唯一的表单名称,您可以用来执行此操作。也许仍然可以通过函数从$ contact_form获取表单ID,但这是可行的,我对结果感到满意。