【问题标题】:Wordpress Contact Form 7, insert dynamic To: address based on meta dataWordpress Contact Form 7,根据元数据插入动态To:地址
【发布时间】:2021-06-27 01:38:05
【问题描述】:

我想向动态收件人发送确认电子邮件。这是在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 );

【问题讨论】:

  • 你回显$women_email 来检查你得到了什么?
  • 在函数中添加回显导致致命错误。

标签: php wordpress forms contact-form-7


【解决方案1】:

Contact Form 7 允许您通过选择位于 Mail 选项卡下的 Mail(2) 来创建自动回复。您需要编辑相关表格: Contact Form 7 Mail Tab, Contact Form 7 Mail(2).

然后您可以使用表单的变量来填充 “To” 字段: Contact Form 7 auto-responder field

【讨论】:

  • 这个怎么插入动态变量?
  • 原始问题指出,“我想向动态收件人发送一封确认电子邮件。” 此功能包含在联系表 7 中. 不必以编程方式执行此操作。
【解决方案2】:

您确定您正确传递了帖子 ID 吗? get_the_ID() 需要 proper context,在这种情况下,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; 

这应该可以解决问题。

【讨论】:

    【解决方案3】:

    在联系表单中,它不会让您获得父帖子的$post->ID。此数据存储在表单的元数据中。这是表单中传递的隐藏字段。

    如果你从前端检查你的表单,你会看到例如:

    <input type="hidden" name="_wpcf7_container_post" value="2730">

    要获取此信息,请访问 $submission->get_meta('$value') 方法。

    这对您有用,因为 women_email 已正确格式化为电子邮件地址。另请注意,before_send_mail 是“动作”而不是“过滤器”

    以下内容未经测试,但应该可以使用。把它放在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 );
    

    【讨论】:

      猜你喜欢
      • 2020-11-24
      • 2023-03-25
      • 2018-11-09
      • 2014-09-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-13
      • 1970-01-01
      相关资源
      最近更新 更多