I have a form with which I get a token through a custom action to authorize an API on another site.
Here’s what the custom token get function looks like:
function my_form_custom_action( $form ) {
$form_data = $form->get_fields();
$login = urlencode($form_data['form-field-smtqfu']);
$password = urlencode($form_data['form-field-crfygb']);
$api_url = 'https://auth.multi.garveks.ru/api/Authenticate/Authenticate?Email=' . $login . '&Password=' . $password;
$response = wp_remote_post( $api_url, array(
'method' => 'POST',
'timeout' => 45,
'redirection' => 5,
'httpversion' => '1.0',
'blocking' => true,
'headers' => array(),
'body' => array(),
'cookies' => array()
)
);
if ( is_wp_error( $response ) ) {
$error_message = $response->get_error_message();
$form->set_result([
'action' => 'my_custom_action',
'type' => 'danger',
'message' => esc_html__('Ошибка: ' . $error_message, 'bricks'),
]);
} else {
$response_body = json_decode( wp_remote_retrieve_body( $response ), true );
if ( $response_body['succeeded'] ) {
$access_token = $response_body['data']['accessToken'];
$redirect_url = 'https://multi.garveks.ru?token=' . $access_token;
$form->set_result([
'action' => 'my_custom_action',
'type' => 'info',
'message' => esc_html__('Ответ: ' . $access_token, 'bricks'),
]);
$form->set_result(
[
'action' => 'my_custom_action',
'type' => 'redirect',
'redirectTo' => $redirect_url,
]
);
return $access_token;
} else {
$form->set_result([
'action' => 'my_custom_action',
'type' => 'danger',
'message' => esc_html__('Ошибка: ' . var_dump($response_body), 'bricks'),
]);
}
}
}
add_action( 'bricks/form/custom_action', 'my_form_custom_action', 10, 1 );
I write the custom action code in function.php
And it works well. If I enter valid data, it prints out a message with a token.
Next, I want to make a redirect, I include an additional action (redirect) in the brick constructor. Next, in the url field, I enter the address of the site of the form (Мультисервис) At the end of this address, I want to substitute the token from the custom function. I add the structure like this (Мультисервис)
But after sending, I get a positive response, a redirect occurs, but at the end of the address it still says {echo: my_form_custom_action}
echo does not print the return value of the function. Although in case of success in the code I write return $access_token?
I thought, maybe the redirect happens faster than the function code works??
I just want to hook to the bricks builder redirect event with my value in the url. Thx