Help redirecting user to parent form view based on entry key provided in API response

By: jason LeRoy | Asked: 10/06/2022
ForumsCategory: Code HelpHelp redirecting user to parent form view based on entry key provided in API response
jason LeRoy asked 2 years ago
Hi. I have a standard form that is configured to create an entry in another form via API. I am trying to find a way to redirect a user to a view for the second form (parent) based on the JSON entry_key value provided in the API response from the first API call. I have tried accomplishing this using the below php code to no avail.
add_action( 'frmapi_post_response', 'frm_save_api_response', 10, 3 );
function frm_save_api_response( $response, $entry, $form_action ) {
$body = json_decode($response['body'], true);
$returned_key = $body['item_key'];
if ( $body['form_id'] == 2) {
header("Location: https://website.com/?entry_key=$returned_key");
) );
}
}
  Below is the body of the API response I receive from the first API call. In this case, I want to use the response to redirect a user to the view at https://website.com/?entry_key=4szaj  
"body": "
{
"id": "198", "item_key": "4szaj", "name": "test 1234", "meta":
{
"ol4jm":
{
"form": "3", "i197":
{
"9j91t": "test", "yg6jl": "2", "x8oih": "test", "9jauh": "4", "o3ajo": "test adsads", "wwko0": "2", "9ou8h": "", "wlpnn": "1", "ekufm": "", "hqbs8": "2.8", "7c3w4": "3.2", "jtg9i": "3.6", "nk7dt": "", "kbx01": "", "yl3nl": "", "wppv4": ""}
},
"9j91t":["test"],"yg6jl":["2"],"x8oih":["test"],"9jauh":["4"],"o3ajo":["test adsads"],"wwko0":["2"],"9ou8h":[""],"wlpnn":["1"],"ekufm":[""],"hqbs8":["2.8"],"7c3w4":["3.2"],"jtg9i":["3.6"],"nk7dt":[""],"kbx01":[""],"yl3nl":[""],"wppv4":[""]
},
"form_id": "2", "post_id": "0", "user_id": "1", "parent_item_id": "0", "is_draft": "0", "updated_by": "1", "created_at": "2022-10-06 02:07:05", "updated_at": "2022-10-06 02:07:05"}", "response":
{
"code":200,"message": "OK"
}
Question Tags:
1 Answers
Best Answer
Victor Font answered 2 years ago
You’re missing something in the action callback. When using the PHP header() function it’s mandatory that you use either the exit() or the die() function immediately after the header redirection to stop script execution and avoid any undesired results.
jason LeRoy replied 2 years ago

Thanks for your assistance! I was able to get this working by adding the exit() function immediately after the header() function, as suggested.

Making the Best WordPress Plugin even better - Together

Take on bigger projects with confidence knowing you have access to an entire community of Formidable Experts and Professionals who have your back when the going gets tough. You got this!
Join the community
crossarrow-right