Php 联系人表单7:使用wpcf7\u创建的钩子,然后通过id仅发送一个联系人表单的邮件
我在一个网站上工作,使用联系人表单7创建了多个表单。对于其中一个表单,我正在传递使用表单中的隐藏输入字段收集的变量。我正在使用wpcf7_before_send_mail hook将这些变量传递到电子邮件中,但这些值正在传递到每封电子邮件中(我添加了动态变量和静态文本),代码如下:Php 联系人表单7:使用wpcf7\u创建的钩子,然后通过id仅发送一个联系人表单的邮件,php,forms,wordpress,contact-form-7,Php,Forms,Wordpress,Contact Form 7,我在一个网站上工作,使用联系人表单7创建了多个表单。对于其中一个表单,我正在传递使用表单中的隐藏输入字段收集的变量。我正在使用wpcf7_before_send_mail hook将这些变量传递到电子邮件中,但这些值正在传递到每封电子邮件中(我添加了动态变量和静态文本),代码如下: add_action( 'wpcf7_before_send_mail', 'wpcf7_add_text_to_mail_body' ); function wpcf7_add_text_to_mail_bod
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。联系人表单7使用隐藏输入类型存储表单id。它使用隐藏字段名\u 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;
}
希望这能有所帮助。我用的是迪内什的答案,但它对我不起作用。相反,我现在正在检查我提交的表单所特有的字段:
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和提交字段的方法发生了变化 要获取表单ID,应使用以下命令:
$form_id = $contact_form->id();
要获取提交数据,您应该使用此选项(而不是$u POST)。函数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
变量的较旧版本的联系人表单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 ) );
}
}
使用
$contact\u form->id()。使用提供给动作挂钩的第三个参数来访问包含类的“发布数据”。该类包含您可以使用的方法。请使用WPCF7_Submission
作为操作挂钩的第三个参数提供。@luukvhoudt谢谢。我已经更新了我的答案,将第三个参数包括在内,因为我在这里的答案发布7个月后,在联系表单7的网站上找到了更新的文档:)您没有向add_action
函数提供wpcf7_before_send_mail
hook的第三个和第四个参数。您至少应该将第4个参数设置为3
,否则如果我没有弄错的话,您将得到一个PHP警告。啊,是的,我忘记添加那个部分了。您是正确的,因为第4个参数的默认值是1
,我们肯定希望通过CF7更新将其更改为3
。
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 ) );
}
}