반응형
워드프레스 API 호출 후 무효 cf7
여기 제 문제가 있습니다. 워드프레스용 연락처 양식 7이 설치되어 있고 wpcf7_before_send_mail 중에 API로 호출하면 양식을 무효화하고 API 호출에서 전달된 오류를 반환해야 합니다.
API 실패 시 플래그를 false로 설정했고 에러 메시지도 저장되어 있으나 내가 유도한 실패에도 불구하고 폼은 성공적으로 진행되고 있습니다.
add_action("wpcf7_before_send_mail", "wpcf7_send_contact_builder");
function wpcf7_send_contact_builder($form) {
$submission = WPCF7_Submission::get_instance();
$wpcf7_data = $submission->get_posted_data();
... api call and set $success to true if ok and false if not ...
if (!$success) {
$form->status = 'validation_failed (statuscode:' . $xml->status->statuscode[0] . ').';
$form->valid = false;
$form->response = $xml->status->statusdesc[0];
return $forml
}
}
또한 다음을 사용해 보았습니다.
$form->invalidate('validation_failed (statuscode:' . $xml->status->statuscode[0] . ').', $xml->status->statusdesc[0]);
그러나 성공적인 이메일이 전송되는 것을 막을 수 없고 양식이 성공적인 것으로 확인됩니다.디버깅을 통해 if 문의 !success가 작동 중이며 포함된 코드가 변수에 추가됨을 증명했습니다.폼도 배열인 것처럼($form['valid'= false) 시도했지만 이것도 작동하지 않았고 폼도 성공한 것으로 나타납니다.여기서 내가 뭘 놓쳤는지 알아요?API 호출 자체에 대한 코드와 정확한 양식 id의 결정을 생략했는데, 이 두 가지가 모두 올바르게 작동합니다. 오직 제가 추구하는 양식만 파싱되고 API 호출은 예상 데이터를 반환합니다.
저도 같은 게 필요했어요.CF7 플러그인 파일을 검토한 결과 다음과 같은 해결책을 발견했습니다.
//To make it working, we must need at least CF7-v5.0;
add_action( 'wpcf7_before_send_mail', 'cf7_validate_api', 15, 3 );
function cf7_validate_api($cf7, &$abort, $submission){
if ( $cf7->id() !== 789 ) //CF7 post-id from admin settings;
return;
$errMsg = '';
//$submission = WPCF7_Submission::get_instance();
$postedData = $submission->get_posted_data();
//$postedData['more-data'] = 'something';
unset($postedData['not-sending-data']);
//-----API posting------
$url = "http://my-web.com/wp-admin/admin-ajax.php?action=get-something";
$username = 'apiUserName';
$password = 'apiUserPass';
$args = [
'headers' => [
'Authorization' => "Basic ".base64_encode( $username . ':' . $password ),
'Accept' => 'application/json; charset=utf-8', // The API returns JSON
//'Content-Type' => 'application/json; charset=utf-8'
],
'body' => $postedData
];
$response = wp_remote_post( $url, $args );
//------------------
if( is_wp_error( $response ) ){
$error_message = $response->get_error_message();
$errMsg = "Something went wrong:\n{$error_message}";
} else {
$response_body = wp_remote_retrieve_body( $response );
$data = json_decode( $response_body );
if( empty($data) || $data->status==0 ){ //API validation error!
$errMsg = $data->msg->title."\n".$data->msg->description;
}
}
if( $errMsg ){ //do not send mail;
//$cf7->skip_mail = true; //for older versions!
$abort = true; //==> Here, it is with 'called by reference' since CF7-v5.0 :)
$submission->set_status( 'validation_failed' );
//$submission->set_response( $cf7->message( 'validation_error' ) ); //msg from admin settings;
$submission->set_response( $cf7->filter_message($errMsg) ); //custom msg;
}
}
누군가에게 도움이 되기를 바랍니다.해피코딩 :)
언급URL : https://stackoverflow.com/questions/36774134/wordpress-invalidate-cf7-after-api-call
반응형
'programing' 카테고리의 다른 글
| ViewPager를 사용하지 않고 탭 레이아웃 (0) | 2023.10.09 |
|---|---|
| 장고 쿼리셋과 함께 파이썬 타입 힌트를 사용하는 방법? (0) | 2023.10.09 |
| 스크롤IntoView는 모든 브라우저에서 작동합니까? (0) | 2023.10.04 |
| j수정 방법 정의되지 않은 '_DT_CellIndex' 속성을 설정할 수 없습니까? (0) | 2023.10.04 |
| AngularJS VS ExtJS (0) | 2023.10.04 |