• I’m trying to set up an API connection. The connection is working fine with my code. I’m having trouble using the error responses though.

    Here is my code:

    
    function on_submit( $form, &$abort, $submission ) {
    
        $data = $submission->get_posted_data();
    
        $errMsg = '';
    
        $email = sanitize_text_field($data['your-email']);
        $name = sanitize_text_field($data['your-name']);
        $phone = sanitize_text_field($data['your-phone']);
        $message = sanitize_textarea_field($data['your-message']);
        
        $apiheaders = array(
            'Content-Type' => 'application/json',
            'Authorization' => 'Token token='.WHC_TOKEN
        );
        $apibody = array( 'marketing_form' => array (
                'email' => $email,
                'first_name' => $name,
                'phone_number' => $phone,
                'notes' => $message,
                'relationship' => 'self',
                'community_id' => WHC_COMMUNITY_ID,
                'lead_source_id' => 60725
            ));
    
        $args_for_push = array (
            'headers' => $apiheaders,
            'body' => json_encode($apibody)
        );
    
        $response = wp_safe_remote_post( WHC_API_URL, $args_for_push );
        $response_code = wp_remote_retrieve_response_code($response);
        $body = wp_remote_retrieve_body($response);
        $result = json_decode($body);
    
        //for debugging issues
        if( boolval( WHC_ENABLE_DEBUG ) ) {
            error_log(print_r($data, true));
            error_log('response code is: '. $response_code);
            error_log(print_r($result, true));
            error_log('apiurl is: '. WHC_API_URL);
            error_log(print_r($args_for_push, true));
            //error_log('submission ARRAY: '.print_r($submission, true));
        }
    
        if ( is_wp_error($response) ) {
            $abort = TRUE;
    
            $submission->set_response($result->error );
            $submission->set_status('api_failed');
        }
        if($response_code != 200) {
            $submission->set_status('api_failed'.$response_code);
            $submission->set_response('Something went wrong: '.$response_code);
        } else {
            $submission->set_status('api_success'.$response_code);
            $submission->set_response('Thank you! we will get back to you shortly. confirm: '.$response_code);
        }
    }

    No matter if the API is successful or not, the form on the front end shows the generic “Thank you for your message. It has been sent.” when the api fails I would like the response and status to change.

    the only time I’ve seen the response change is if I make a submission on the form on the front end and then make a 2nd one before refreshing the page. (so the alert at the bottom “Thank you for your message. It has been sent.” is still there on the 2nd submission.

    I’ve looked all over for documentation on the hooks and it continues to elude me.

  • The topic ‘setting status and response with before_send_mail’ is closed to new replies.