【问题标题】:Change recipient based on Contact Form 7 dropdown根据联系表 7 下拉列表更改收件人
【发布时间】:2022-10-22 18:47:09
【问题描述】:

我在 WordPress 网站上有一个使用联系表格 7 的表格。我有一个下拉列表来选择收件人,但我不想在此处列出电子邮件地址。

收件人从自定义帖子类型中列出,提交表单时,我需要根据选择名称查找电子邮件地址。我有以下代码,但它没有改变收件人。

function wpcf7_before_send_mail_function( $contact_form, $abort, $submission ) {
   $submission = WPCF7_Submission::get_instance(); 
   $posted_data = $submission->get_posted_data(); 
   if( $posted_data["your-recipient"] == 'General Enquiry' ) { 
      $recpage = get_page_by_title('James');
      $recipient_email = $recpage->email_address;
   } else {
      $recpage = get_page_by_title($posted_data["your-recipient"]);
      $recipient_email = $recpage->email_address;
   }
   $properties = $contact_form->get_properties();
   $properties['mail']['recipient'] = $recipient_email;
   $contact_form->set_properties($properties);
   return $contact_form;
}
add_filter( 'wpcf7_before_send_mail', 'wpcf7_before_send_mail_function', 10, 3 );

知道为什么这不会改变收件人地址吗?谢谢。

【问题讨论】:

  • 如果$posted_data["your-recipient"] 是一个下拉菜单,那么您想使用$posted_data["your-recipient"][0]
  • 谢谢霍华德,这似乎是正确的,但仍然没有发送 - 如果我删除所有“get_page_by_title”代码并只输入电子邮件地址,它仍然不会改变收件人,所以认为还有其他问题或者根本没有触发该功能。
  • 你不需要返回任何东西...... wpcf7_before_send_mail 是一个动作而不是过滤器。尝试更改为 add_action
  • 谢谢霍华德 - 我从另一篇文章中注意到这一点并改变了这一点。看起来该过程拒绝发送到任何未注明为站点管理员的地址 - 假设是安全的事情,但有点问题,因为并非所有地址都用于站点域。将对此进行调查
  • Howard - 我意识到 get_page_by_title 在寻找自定义帖子类型时需要额外的参数 - 现在一切都很好。如果您可以添加您的初始建议作为回复,我将批准它作为解决方案 - 再次感谢

标签: php wordpress hook contact-form-7


【解决方案1】:

自 2020 年左右使用 CF7 以来,您的选择数据以数组形式发布。因此,您需要访问该数组的第一个值 [0] 以获取下拉值。此外,正如您所评论的,您需要get_page_by_title 的第三个参数,最后您不需要在函数中使用return,因为它会执行您的代码并根据需要对对象应用更改。 wpcf7_before_send_mail 是一个动作钩子,而不是一个过滤器。

function wpcf7_before_send_mail_function( $contact_form, $abort, $submission ) {
    $posted_data = $submission->get_posted_data();
    if ( 'General Enquiry' === $posted_data['your-recipient'][0] ) {
        $recpage         = get_page_by_title( 'James', OBJECT, 'your_CPT' );
        $recipient_email = $recpage->email_address;
    } else {
        $recpage         = get_page_by_title( $posted_data['your-recipient'][0], OBJECT, 'your_CPT' );
        $recipient_email = $recpage->email_address;
    }
    $properties                      = $contact_form->get_properties();
    $properties['mail']['recipient'] = $recipient_email;
    $contact_form->set_properties( $properties );
    // Do not return anything, since this is an action and not a filter.
}
add_filter( 'wpcf7_before_send_mail', 'wpcf7_before_send_mail_function', 10, 3 );

【讨论】:

    猜你喜欢
    • 2016-02-19
    • 1970-01-01
    • 1970-01-01
    • 2018-03-06
    • 2018-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多