I'm using a custom wordpress endpoint that I have defined in functions.php:
add_action('rest_api_init', function () {
register_rest_route('custom_endpoint', 'submit-application', [
'methods' => 'POST',
'callback' => 'submit_application'
]);
});
I'm using the endpoint to process some form data sent via jetform builder. After the form data has been processed (various calls to an external api), I would like to redirect to a url that is returned by the api dynamically. Currently the business logic all succeeds, but I cannot for the life of me get the redirect to work. The form submits and displays a message "Form successfully submitted".
function submit_application ( $request ) {
//Business logic here all succeeds
$user= send_data($data);
try {
return new \WP_REST_Response(
null,
303,
array(
'Location' => $user['url'],
)
);
} catch (Exception $e) {
return new WP_REST_Response( [
'status' => 'error',
'message' => $e->getMessage()
], 400 );
}
}
I've also tried using
wp_redirect( $user['url'] );
exit();
The odd thing is I got it to work once, with what I think is this exact code, but suddenly after I continued development it stopped. It is also odd, because when I try the end point via postman instead of the form submission, it seems to receive the redirect correctly as well. Maybe that's indicative of a cache issue? I'm a little new to wordpress so any ideas would be very welcome!