隐藏电子邮件中的空白字段 - Contact Form 7
Hide empty fields in email - Contact Form 7
我花了将近一整天的时间寻找答案,但没有任何帮助..
我用 Contact Form 7 制作了一个大表格,但部分表格会隐藏,具体取决于您的选择。例如,如果您选择“2 人”,则会显示两个部分。
但是,如果我为一个人填写字段(因此其他字段为空且隐藏),这些字段将在电子邮件中可见。我只想查看电子邮件中填写的字段。
对不起,如果我有点不清楚。需要一些帮助,请。
尝试将此添加到您的 functions.php 文件中:
add_filter( 'wpcf7_mail_components', 'remove_blank_lines' );
function remove_blank_lines( $mail ) {
if ( is_array( $mail ) && ! empty( $mail['body'] ) )
$mail['body'] = preg_replace( '|\n\s*\n|', "\n\n", $mail['body'] );
return $mail;
}
我在此处找到了代码段:https://wordpress.org/support/topic/plugin-contact-form-7-how-to-do-away-with-blank-lines-in-email-for-unfilled-form-items/
根据您设置电子邮件的方式,这可能不起作用,因为这只会删除空行。让我知道这是否适合您,如果不适合,请提供您电子邮件正文中的代码,发送一封示例电子邮件也很好。
实际上您需要使用
实现您自己的自定义电子邮件正文组件
add_filter('wpcf7_mail_components','my_custom_mail', 10,2);
function my_custom_mail($mail_component, $contact_form){
$mail_component['subject']; //email subject
$mail_component['sender']; //sender field (from)
$mail_component['body']; //email body
$mail_component['recipient']; //email recipient (to)
$mail_component['additional_headers']; //email headers, cc:, bcc:, reply-to:
$mail_component['attachments']; //file attachments if any
$key_values = array();
$tags = $contact_form->scan_form_tags(); //get your form tags
foreach($tags as $tag){
$field_name = $tag['name'];
if(isset($_POST[$field_name]) && !empty($_POST[$field_name])){
//get all the submitted fields form your form
$key_values[$field_name] = $_POST[$field_name];
}
}
//you have all the submitted field-name => value pairs in the array $key_values
//you can now reset you email body
$body = "Dear ".$key_values['your-name'].',';
...
$mail_component['body'] = $body;
return $mail_component;
}
已解决!
我自己找到了解决方案,在Contact Form 7中已经有了。e-mail中的字段不在同一行,所以当我检查"Exclude lines with blank mail-tags from output"时,没有任何反应。我已将它们全部放在同一条线上,现在可以使用了。
我花了将近一整天的时间寻找答案,但没有任何帮助..
我用 Contact Form 7 制作了一个大表格,但部分表格会隐藏,具体取决于您的选择。例如,如果您选择“2 人”,则会显示两个部分。
但是,如果我为一个人填写字段(因此其他字段为空且隐藏),这些字段将在电子邮件中可见。我只想查看电子邮件中填写的字段。
对不起,如果我有点不清楚。需要一些帮助,请。
尝试将此添加到您的 functions.php 文件中:
add_filter( 'wpcf7_mail_components', 'remove_blank_lines' );
function remove_blank_lines( $mail ) {
if ( is_array( $mail ) && ! empty( $mail['body'] ) )
$mail['body'] = preg_replace( '|\n\s*\n|', "\n\n", $mail['body'] );
return $mail;
}
我在此处找到了代码段:https://wordpress.org/support/topic/plugin-contact-form-7-how-to-do-away-with-blank-lines-in-email-for-unfilled-form-items/
根据您设置电子邮件的方式,这可能不起作用,因为这只会删除空行。让我知道这是否适合您,如果不适合,请提供您电子邮件正文中的代码,发送一封示例电子邮件也很好。
实际上您需要使用
实现您自己的自定义电子邮件正文组件 add_filter('wpcf7_mail_components','my_custom_mail', 10,2);
function my_custom_mail($mail_component, $contact_form){
$mail_component['subject']; //email subject
$mail_component['sender']; //sender field (from)
$mail_component['body']; //email body
$mail_component['recipient']; //email recipient (to)
$mail_component['additional_headers']; //email headers, cc:, bcc:, reply-to:
$mail_component['attachments']; //file attachments if any
$key_values = array();
$tags = $contact_form->scan_form_tags(); //get your form tags
foreach($tags as $tag){
$field_name = $tag['name'];
if(isset($_POST[$field_name]) && !empty($_POST[$field_name])){
//get all the submitted fields form your form
$key_values[$field_name] = $_POST[$field_name];
}
}
//you have all the submitted field-name => value pairs in the array $key_values
//you can now reset you email body
$body = "Dear ".$key_values['your-name'].',';
...
$mail_component['body'] = $body;
return $mail_component;
}
已解决!
我自己找到了解决方案,在Contact Form 7中已经有了。e-mail中的字段不在同一行,所以当我检查"Exclude lines with blank mail-tags from output"时,没有任何反应。我已将它们全部放在同一条线上,现在可以使用了。