В WordPress, как установить пользовательское ответное сообщение для CF7 - PullRequest
0 голосов
/ 21 января 2020

Я хочу установить пользовательское ответное сообщение для CF7. После отправки через CF7 я получу вывод ответа от метода wp_remote_post, но не смог отобразить сообщение об ошибке ответа в форме CF7. У меня есть следующий код. Пожалуйста, объясните мне, как я могу установить сообщение об ошибке пользовательского ответа.

add_filter( 'wpcf7_before_send_mail', 'create_tocken' );

function create_tocken( $contact_form ) {
    global $wpdb;

    if ( ! isset( $contact_form->posted_data ) && class_exists( 'WPCF7_Submission' ) ) {
        $submission = WPCF7_Submission::get_instance();

        if ( $submission ) {
            $form_data = $submission->get_posted_data();
        }
    } else {
        return $contact_form;
    }

    $body = array(
        'username' => 'xxxxx',
            'password' => 'xxxxxx',
            'type' => 'xxxxx',
            'name' => 'xxxxxx',             
            'phone' => 'xxxxxx', 
            'email' => 'xxxxxx',               
            'town' => 'xxxxxx', 
    );

    $url = 'https://example.com/';

    $params = array(
        'headers' => array(
            'Content-Type' => 'application/x-www-form-urlencoded'
        ),
        'body' => $body
    );

    $response = wp_remote_post( $url,  $params );

    if ( is_wp_error( $response ) ) {
        $error_message = $response->get_error_message();

    }


}

Здесь я хотел установить $ error_message в качестве сообщения пользовательского ответа.

1 Ответ

0 голосов
/ 30 января 2020

Следующее решение решило эту проблему.

add_action('wpcf7_before_send_mail', 'cf7_validate_api', 10, 3);


function cf7_validate_api($cf7, &$abort, $submission) {

    if ($cf7->id() !== 887) 
    {
        return;
    }

    $errMsg = '';

    $submission = WPCF7_Submission::get_instance();
    $postedData = $submission->get_posted_data();
    $fields = [];
    $api_username = 'xxxxxxx';
    $api_key = 'xxxxxxx';
    $api_url = 'xxxxxxx';

    $fields['username'] = $api_username;
    $fields['key'] = $api_key;
    $fields['endpoint'] = 'test';
    $fields['firstname'] = $postedData['firstname'];
    $fields['lastname'] = $postedData['lastname'];
    $fields['phone'] = $postedData['phone'];
    $fields['email'] = $postedData['email'];

    $params = [
        'headers' => [
            'Content-Type' => 'application/x-www-form-urlencoded',
        ],
        'body' => $fields,
    ];
    $response = wp_remote_post($api_url, $params);  

    if (is_wp_error($response)) {
        $error_message = $response->get_error_message();
        $errMsg = "Something went wrong:\n{$error_message}";

    } else {
        $api_result = json_decode($response['body'], true);
        if ($api_result['success'] == 'false') {
            $errMsg = $api_result['message'];
        }
    }

    if ($errMsg) {
        $abort = true;
        $submission->set_response($cf7->message('validation_failed'));
        $submission->set_response($cf7->filter_message($errMsg)); //custom msg;
    }

}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...