I’ve written this integration for Mailerlite and set it up for one of my forms:
<?php
function my_form_custom_action( $form ) {
$form_fields = $form->get_fields();
$form_id = $form_fields['formId'];
if( $form_id !== 'yzrfqi' ) return;
$email_address = $form->get_field_value( 'lqmhlc' );
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api.mailerlite.com/api/v2/groups/XXXXXXXX/subscribers/",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'email' => $email_address,
'enable' => true
]),
CURLOPT_HTTPHEADER => [
"X-MailerLite-ApiDocs: true",
"X-MailerLite-ApiKey: XXXXXXXXXXXXXXXXXXXXX",
"accept: application/json",
"content-type: application/json"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
return $err;
} else {
return $response;
}
// Set result in case it fails
$form->set_result([
'action' => 'my_custom_action',
'type' => 'success', //or danger or info
'message' => esc_html__('There was an error. Please contact us via email', 'bricks'),
]);
}
add_action( 'bricks/form/custom_action', 'my_form_custom_action', 10, 1 );
?>
It works like a chame. But during some testing I found out that Bricks doesn’t recognise if an error response happens. I tested with a dummy API key that would have returned an error and Bricks still thought everything went through correctly. How do I get Bricks recognise and output the error, if it happens?