Created
September 19, 2021 08:07
-
-
Save MjHead/6771eaba3adfe250378b4408878e75c5 to your computer and use it in GitHub Desktop.
JetFormBuilder. Example of custom hook with API request and redirect.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* 'api-request' - is a customm hook name | |
*/ | |
add_action( 'jet-form-builder/custom-action/api-request', function( $request, $action_handler ) { | |
$post_id = ! empty( $request['inserted_post_id'] ) ? $request['inserted_post_id'] : false; | |
if ( ! $post_id ) { | |
return; | |
} | |
$response = wp_remote_get( 'https://fakerapi.it/api/v1/companies?_quantity=1' ); | |
$body = wp_remote_retrieve_body( $response ); | |
$body = json_decode( $body, true ); | |
if ( $body ) { | |
$data = $body['data'][0]; | |
update_post_meta( $post_id, 'api-response', $data['name'] ); | |
} | |
$redirect = get_permalink( $post_id ); | |
if ( ! $request['__is_ajax'] ) { | |
wp_safe_redirect( $redirect ); | |
die(); | |
} else { | |
$action_handler->response_data['redirect'] = $redirect; | |
} | |
}, 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Good to know:
I love these plugins, I hate their documentation. Hope this helps. ;-)