联系表格 7:使用 wpcf7_before_send_mail 创建的钩子仅用于一个联系表格的 id [英] Contact Form 7: use hook created using wpcf7_before_send_mail for only one contact form by id

查看:36
本文介绍了联系表格 7:使用 wpcf7_before_send_mail 创建的钩子仅用于一个联系表格的 id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个站点上工作,该站点具有使用联系表单 7 创建的多个表单.对于这些表单之一,我正在传递我使用表单中的隐藏输入字段收集的变量.我使用 wpcf7_before_send_mail 钩子将这些变量传递到电子邮件中,但这些值传递到每封电子邮件中(我添加了动态变量以及静态文本)这是代码:

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:

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.

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.

推荐答案

Contact Form 7 使用隐藏输入类型来存储表单 ID.它使用隐藏字段名称_wpcf7.您可以通过这种方式获取表单 Id.

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.

$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;

}

希望这会有所帮助.

这篇关于联系表格 7:使用 wpcf7_before_send_mail 创建的钩子仅用于一个联系表格的 id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆