联系表 7 提交验证失败

Contact Form 7 submit failed validation

我需要 Contact Form 7 在验证失败 时发送一封电子邮件。然后在正确提交表单后发送正常的 CF7 电子邮件。我知道这很荒谬,但是 客户

我认为我比较接近以下几点:

function send_failed_vaildation_email( $data ) {
    $messagesend = 'Name:' . $_POST['your-name']; 
    $messagesend .= '\r\nEmail:' .$_POST['email']; 
    $messagesend .= '\r\nPhone:' .$_POST['validPhone']; 
    $messagesend .= '\r\nRate:' .$_POST['rate']; 
    $messagesend .= '\r\nBased:' .$_POST['based']; 
    wp_mail('c******y@gmail.com', 'failed validation mail', $messagesend );
}
add_filter("wpcf7_posted_data", "send_failed_vaildation_email");

然而,这将发送所有提交,无论它们是否通过验证。

wpcf7_before_send_mail 不好,因为它只会在提交通过验证后触发。

我正在寻找一个不同的钩子来代替 wpcf7_posted_data,它只在验证失败时触发,或者我可以在 wp_mail 周围放置一个 if 语句以获得相同的效果。

提前致谢

这尚未经过测试,但我很确定您需要连接到 get_invalid_fields() 实例。类似于:

function send_failed_vaildation_email() {
    $submission = WPCF7_Submission::get_instance();
    $invalid_fields = $submission->get_invalid_fields();

    $posted_data = $submission->get_posted_data();

    if ( !empty( $invalid_fields ) ) {
        $messagesend = 'Name:' . $posted_data['your-name']; 
        $messagesend .= '\r\nEmail:' . $posted_data['email']; 
        $messagesend .= '\r\nPhone:' . $posted_data['validPhone']; 
        $messagesend .= '\r\nRate:'  . $posted_data['rate']; 
        $messagesend .= '\r\nBased:' . $posted_data['based']; 
        wp_mail('c******y@gmail.com', 'failed validation mail', $messagesend );
    }
}
add_filter("wpcf7_posted_data", "send_failed_vaildation_email");

可以看到表单提交过程in the plugin trac

编辑: 我也刚刚注意到 'wpcf7_validation_error' 挂钩 contact-form.php...这可能就是您所需要的,因为它只在有一个错误。

这就是我设法让它工作的方法。

@rnevius 回答失败,因为在验证数据之前应用了 wpcf7_posted_data 过滤器,因此当时没有任何内容是无效的。

将@rnevius 的回答与 wpcf7_submit 过滤器相结合,它按预期工作。

完整代码:

function send_failed_vaildation_email() {
    $submission = WPCF7_Submission::get_instance();
    $invalid_fields = $submission->get_invalid_fields();

    $posted_data = $submission->get_posted_data();

    if ( !empty( $invalid_fields ) ) {
        $messagesend = 'Name:' . $posted_data['your-name']; 
        $messagesend .= '\r\nEmail:' . $posted_data['email']; 
        $messagesend .= '\r\nPhone:' . $posted_data['validPhone']; 
        $messagesend .= '\r\nRate:'  . $posted_data['rate']; 
        $messagesend .= '\r\nBased:' . $posted_data['based']; 
        $messagesend .= count($invalid_fields); 
        wp_mail('c*******y@gmail.com', 'failed validation mail', $messagesend );
    }
}
add_filter("wpcf7_submit", "send_failed_vaildation_email");

最新联系表 7 的更新代码

function is_gmail($email) {  
if(substr($email, -10) == '@gmail.com') {  
   return true;  
} else {  
   return false;  
};  
};  

function custom_email_validation_filter($result, $tag) {  
$tag = new WPCF7_Shortcode( $tag );
if ( 'your-email' == $tag->name ) {
    $the_value = isset( $_POST['your-email'] ) ? trim( $_POST['your-email'] ) : '';
    if(!is_gmail($the_value)){  
             $result->invalidate( $tag, "Are you sure this is the correct address?" );
   };  
 };  
 return $result;  
};  


add_filter('wpcf7_validate_email','custom_email_validation_filter', 20, 2); // Email field  
add_filter('wpcf7_validate_email*', 'custom_email_validation_filter', 20, 2); // Required Em