php 联系表格 7:使用 wpcf7_before_send_mail 创建的钩子仅用于一个联系表格的 id
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30533216/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Contact Form 7: use hook created using wpcf7_before_send_mail for only one contact form by id
提问by ardev
I am working on a site with several forms created using Contact Form 7. For one of these forms, I am passing variables that I collected using a hidden input field in the form. I am passing these variables into the email using the wpcf7_before_send_mail hook, but these values are passing into every email (I added dynamic variables as well as static text) Here's the code:
我在一个站点上工作,其中包含使用 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 ) );
}
I am trying to figure out how to only pass these values to one of the contact form email templates, probably via the form id.
我试图弄清楚如何仅将这些值传递给联系表单电子邮件模板之一,可能是通过表单 ID。
回答by Dinesh Karki
Contact Form 7 uses hidden input type to store form id. It uses hidden field name _wpcf7. You can get the form Id like this way.
Contact Form 7 使用隐藏输入类型来存储表单 ID。它使用隐藏字段名称_wpcf7。您可以通过这种方式获取表单 Id。
$form_id = $contact_form->posted_data['_wpcf7'];
So you final code should be
所以你最终的代码应该是
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;
}
Hope this helps.
希望这可以帮助。
回答by Jarom
I was using Dinesh's answer, but it stopped working for me. Instead, I am now checking for a field that is unique to the form I'm submitting:
我正在使用 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
}
}
Be sure to have at least one unique form name in each of your forms that you can use to do this. It might still be possible to get the form ID from $contact_form via a function, but this worked and I was content with the result.
确保在您可以用来执行此操作的每个表单中至少有一个唯一的表单名称。仍然可以通过函数从 $contact_form 获取表单 ID,但这有效,我对结果感到满意。
回答by Tessa
The methods for retrieving the form ID and submitted fields have changed in this plugin since 2015.
自 2015 年以来,此插件中检索表单 ID 和提交字段的方法发生了变化。
To get the form ID, you should use this:
要获取表单 ID,您应该使用:
$form_id = $contact_form->id();
To get the submission data you should use this (instead of $_POST):
要获取提交数据,您应该使用它(而不是 $_POST):
$submission = WPCF7_Submission::get_instance();
$posted_data = $submission->get_posted_data();
To put it all together, your snippet would look like this:
总而言之,您的代码段将如下所示:
add_action( 'wpcf7_before_send_mail', 'wpcf7_add_text_to_mail_body' );
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();
$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 ) );
}
}