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 ); |
Good to know:
- $request is a multi dimensional array that consists of array keys that correspond with the IDs of the inputs in your form.
- If you need the ID of the CCT that will be created, you need to look for the key "inserted_cct_" + slug of your CCT. So if your CCT is called "record_information", then the name of the key in the $request will be "inserted_cct_record_information".
- your form needs to include an action to call a custom hook; the name of the hook needs to correspond with the last part of the action hook you defined. So if we're following the example above, you will need to enter the value "api-request" in the custom hook settings for your JetFormBuilder.
I love these plugins, I hate their documentation. Hope this helps. ;-)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You have to write a php code like this:
`
function myfunctionname(){
YOUR PHP CODE GOES HERE
}
add_action('jet-form-builder/custom-action/actionname',myfunctionname, 10, 2);
`
you should use a child theme and add that code to its functions.php otherwise when you update your theme, this code will be removed.