2017-08-12 177 views
0

我试图改变API调用后联系表单7的结果的状态,所以如果需要我可以在前端返回一个错误(即默认情况下,它应该在表单下显示一个红色错误从Ajax响应)动态更改CF7状态

我使用Forms3rdPartyIntegration插件,但这只是给了我一个回调挂钩,我可以再尝试改变CF7输出(https://github.com/zaus/forms-3rdparty-integration

至于我能看到CF7状态是只读的?我看不到的方式来阻止CF7只是给一个mail_sent_ok状态

add_action('Forms3rdPartyIntegration_service', array(&$this, 'service_callback'), 10, 2); 

public function service_callback($response, $results) {   

    $submission = WPCF7_Submission::get_instance(); 
    $cf7 = WPCF7_ContactForm::get_current(); 

    // check for errors (code omitted) 
    // this is what I am essentially trying to do 
    // but doesn't work 
    $submission->status = 'mail_failed' 
    $cf7->skip_mail = true; 

    ... 
} 

我会很感激,如果任何人对触发CF7失败响应任何指针。

这似乎是一个类似的问题 wordpress invalidate cf7 after api call

回答

1

我知道这是一个老问题,但任何人谁遇到这个问题,我认为你正在寻找这样的:

if(your_condition) { 
    add_filter("wpcf7_ajax_json_echo", function ($response, $result) { 
     $response["status"] = "mail_sent_ng"; 
     $response["message"] = "Validation errors occurred. Please confirm the fields and submit it again."; 
     return $response; 
    }); 
} 

这使状态mail_sent_ng而不是mail_ok_sent

另外$response["message"]设置显示给用户的错误/ ajax消息。

由于您的代码中已经有$cf7->skip_mail = true,因此您已停止发送邮件,并且使用上面的代码,您向用户显示有错误。

您也可以使用状态validation_error