Skip to content

Instantly share code, notes, and snippets.

@MjHead
Created September 19, 2021 08:07
Show Gist options
  • Save MjHead/6771eaba3adfe250378b4408878e75c5 to your computer and use it in GitHub Desktop.
Save MjHead/6771eaba3adfe250378b4408878e75c5 to your computer and use it in GitHub Desktop.
JetFormBuilder. Example of custom hook with API request and redirect.
<?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 );
@jacktaffa
Copy link

$request['inserted_post_id'] it doesn't work anymore on v 1.5.2

@lifeact
Copy link

lifeact commented Jul 6, 2022

Hi, I try:

`
add_action( 'jet-form-builder/custom-action/m_zayavka_popup', function($request) {
$secret = 'a8******11';
$dataMsg = [
'secret' => $secret,
'message' => 'ФИО: ' . $request['field_name'] . ' Тел: ' . $request['name_phone'] . ' Адреса: '. $request['adress']
];

$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => 'https://bot.******/api/sendNotification',
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => http_build_query($dataMsg)
]);
curl_exec($curl);
curl_close( $curl );
}
);
`

all ok, action email – work\send, action hook – work, but “Form successfully submitted” – not working…. message not showed….
Any tips?

@lifeact
Copy link

lifeact commented Jul 6, 2022

reolace curl with:

`
$secret = 'a8333333333331';
$url = 'https://33333333333/api/sendNotification';
$args = array(
'timeout' => 45,
'method' => 'POST',
'body' => array('secret' => $secret, 'message' => 'ФИО: ' . $request['field_name'] . ' Тел: ' . $request['name_phone'] . ' Адр: '. $request['adress'] )
);

$response = wp_remote_post( $url, $args );
`

and noe notify work

@gezzeg
Copy link

gezzeg commented Jun 17, 2023

Hi, may i have some question here

  1. Sorry i am new to wordpress hook. I have read the documentation, but couldnt understand. Where to put the action code? Is it i need to create the directory as "jet-form-builder/custom-action/actionname"?
  2. I want to call this action hook from popup button. Is it possible? Thank you.

@heisenberg0924
Copy link

Hi, may i have some question here

  1. Sorry i am new to wordpress hook. I have read the documentation, but couldnt understand. Where to put the action code? Is it i need to create the directory as "jet-form-builder/custom-action/actionname"?
  2. I want to call this action hook from popup button. Is it possible? Thank you.

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.

@nextab
Copy link

nextab commented Oct 31, 2023

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