根据联系人表格7下拉列表更改收件人



我在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 );

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

您的选择数据将作为数组发布,大约从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 );

最新更新