Contact Form 7 获取 post 表单页面 ID 以发挥作用
Contact Form 7 get post ID of form page to function
我想向动态收件人发送确认电子邮件。这在 functions.php 文件中。下面的代码有效,但是当我尝试用变量替换静态电子邮件时,出现错误。如何正确查询 post_meta
women_email 并动态插入它来代替静态 gmail 地址?
示例页面:https://www.ohioacc.org/women/sandra-nichols/
function wpcf7_before_send_mail_function( $contact_form, $abort, $submission ) {
$women_email = get_post_meta( get_the_ID(), 'women_email', true);
//$dynamic_email = $women_email; THIS DOES NOT WORK
$dynamic_email = "myemail@gmail.com"; //THIS WORKS
$properties = $contact_form->get_properties();
$properties['mail']['recipient'] = $dynamic_email;
$contact_form->set_properties($properties);
return $contact_form;
}
add_filter( 'wpcf7_before_send_mail', 'wpcf7_before_send_mail_function', 10, 3 );
Contact Form 7 允许您通过选择 Mail(2) 创建自动回复,它位于 Mail 选项卡下 .您需要编辑相关表格:
Contact Form 7 Mail Tab,
Contact Form 7 Mail(2).
然后您可以使用表单的变量来填充 "收件人" 字段:
Contact Form 7 auto-responder field
您确定您传递的 post ID 正确吗?
get_the_ID()
需要 proper context, in this case, the WP Loop
我假设这是您 functions.php 中的一个函数。
// Gets global $post object
global $post;
$email = get_post_meta( $post->ID, 'women_email', true);
$properties = $contact_form->get_properties();
$properties['mail']['recipient'] = $email;
$contact_form->set_properties($properties);
return $contact_form;
这应该可以解决问题。
在联系表中,它不会让您获得父 post 的 $post->ID
。此数据存储在表单的元数据中。它是表单中传递的隐藏字段。
如果你从前端检查你的表单,你会看到这个例如:
<input type="hidden" name="_wpcf7_container_post" value="2730">
要获得它,您需要访问 $submission->get_meta('$value')
方法。
如果 women_email
的电子邮件地址格式正确,这对您有用。另请注意 before_send_mail
是“ACTION”而不是“FILTER”
以下未经测试,但应该有效。把这个放在 functions.php
function wpcf7_before_send_mail_function($contact_form, &$abort, $submission){
// This gets your post_id
$post_id = $submission->get_meta('container_post_id');
$properties = $contact_form->get_properties();
$properties['mail']['recipient'] = get_post_meta( $post_id, 'women_email', true);
$contact_form->set_properties($properties);
}
add_action( 'wpcf7_before_send_mail', 'wpcf7_before_send_mail_function', 10, 3 );
我想向动态收件人发送确认电子邮件。这在 functions.php 文件中。下面的代码有效,但是当我尝试用变量替换静态电子邮件时,出现错误。如何正确查询 post_meta
women_email 并动态插入它来代替静态 gmail 地址?
示例页面:https://www.ohioacc.org/women/sandra-nichols/
function wpcf7_before_send_mail_function( $contact_form, $abort, $submission ) {
$women_email = get_post_meta( get_the_ID(), 'women_email', true);
//$dynamic_email = $women_email; THIS DOES NOT WORK
$dynamic_email = "myemail@gmail.com"; //THIS WORKS
$properties = $contact_form->get_properties();
$properties['mail']['recipient'] = $dynamic_email;
$contact_form->set_properties($properties);
return $contact_form;
}
add_filter( 'wpcf7_before_send_mail', 'wpcf7_before_send_mail_function', 10, 3 );
Contact Form 7 允许您通过选择 Mail(2) 创建自动回复,它位于 Mail 选项卡下 .您需要编辑相关表格: Contact Form 7 Mail Tab, Contact Form 7 Mail(2).
然后您可以使用表单的变量来填充 "收件人" 字段: Contact Form 7 auto-responder field
您确定您传递的 post ID 正确吗?
get_the_ID()
需要 proper context, in this case, the WP Loop
我假设这是您 functions.php 中的一个函数。
// Gets global $post object
global $post;
$email = get_post_meta( $post->ID, 'women_email', true);
$properties = $contact_form->get_properties();
$properties['mail']['recipient'] = $email;
$contact_form->set_properties($properties);
return $contact_form;
这应该可以解决问题。
在联系表中,它不会让您获得父 post 的 $post->ID
。此数据存储在表单的元数据中。它是表单中传递的隐藏字段。
如果你从前端检查你的表单,你会看到这个例如:
<input type="hidden" name="_wpcf7_container_post" value="2730">
要获得它,您需要访问 $submission->get_meta('$value')
方法。
如果 women_email
的电子邮件地址格式正确,这对您有用。另请注意 before_send_mail
是“ACTION”而不是“FILTER”
以下未经测试,但应该有效。把这个放在 functions.php
function wpcf7_before_send_mail_function($contact_form, &$abort, $submission){
// This gets your post_id
$post_id = $submission->get_meta('container_post_id');
$properties = $contact_form->get_properties();
$properties['mail']['recipient'] = get_post_meta( $post_id, 'women_email', true);
$contact_form->set_properties($properties);
}
add_action( 'wpcf7_before_send_mail', 'wpcf7_before_send_mail_function', 10, 3 );