How To Set Up Bricks With Webhooks – Step By Step

Custom Action Function 👇

/**
 * Function to Send Bricks Form Data to Webhook
 */
function pro_bricks_form_to_webhook( $form ){

	$data = $form->get_fields();

	// The Bricks Form ID is the last part of the CSS ID
	// So if a form has a CSS ID of bricks-element-fszxsr, then the form ID is fszxsr
	$formId = $data['formId'];

	// Replace this comment with the description of the form
	// Copy and paste the IF block below to add custom action for other forms
	if( $formId == 'npujhy' ){
		
		// change the array to strings joined by ,
		$data['form-field-gilnan'] = cleanFormArray($data['form-field-gilnan']);
	
	        $curl = curl_init("https://some-web-hook-url-goes-here");
		curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
		curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
		$result = curl_exec($curl);

		// Set result of action
		if( !$result ){
			$form->set_result([
				'action' => 'my_custom_action',
				'type'    => 'danger', //or danger or info
				'message' => esc_html__('Webhook failed', 'bricks'),
			]);
		}
	}
	
 }
 add_action( 'bricks/form/custom_action', 'pro_bricks_form_to_webhook', 10, 1 );

Initial snippet provided by Chris Castillo

cleanFormArray Function 👇

function cleanFormArray(array $array) {
  // Remove the square brackets and parentheses.
    $newArray = array_map(function($item) {
        return trim(preg_split('/[[]|()]/', $item)[0], ' ');
    }, $array);

  // Implode the array, separated by commas.
    return implode(', ', $newArray);
}

Since making this tutorial, I’ve moved to using Make. I find it to be much more versatile and flexible, and I plan to create many tutorials on how to use it.