联系表格7提交验证失败

时间:2015-03-05 10:51:24

标签: php wordpress wordpress-plugin contact-form-7

当验证失败时,我需要联系表单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语句以获得相同的效果。

提前致谢

3 个答案:

答案 0 :(得分:1)

这还没有经过测试,但我很确定你需要挂钩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

编辑:我刚刚注意到contact-form.php中的'wpcf7_validation_error'挂钩...这可能就是您所需要的一切,因为它只会在出现错误时触发。

答案 1 :(得分:1)

这就是我设法让它发挥作用的方式。

@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");

答案 2 :(得分:0)

最新联系表格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
相关问题