How to use webhooks with forms

I am looking to send a form’s information to a webhook (Pabbly) whenever a user registers for an online course.

I saw this: SOLVED: Custom setting and execution of a form but I don’t know much about PHP so I was wondering if someone could be so kind as to help me put it together.

Pabbly gives a webhook link (e.g. www.pabbly.com/mycoolwebhooklink/). Upon submition, send all that info to the webhook, where then I can have Pabbly parse and map it to my Acumbamail list fields.

Please let me know at your earliest convenience so that I can use it for my site and then dissect the code to learn.

Thank you kindly!

1 Like

Ok, please let me know if I am doing this correctly and won’t open up my system to being hacked:

<?php 

function my_form_custom_action( $form ) {
   
  $fields = $form->get_fields();
  // $formId = $fields['formId'];
  // $postId = $fields['postId'];
  // $settings = $form->get_settings();
  // $files = $form->get_uploaded_files();
  
  // Perform some logic here...
  $webhook_url = 'https://connect.pabbly.com/workflow/sendwebhookdata/XXXXXXXXXXXX';
  $curl = curl_init($webhook_url);
  $jsonDataEncoded = json_encode($fields);
  curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($curl, CURLOPT_POSTFIELDS, $jsonDataEncoded);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    return curl_exec($curl);

  // Set result in case it fails
  $form->set_result([
    'action' => 'my_custom_action',
    'type'    => 'success', //or danger or info
    'message' => esc_html__('Oh my custom action failed', 'bricks'),
  ]);
}
add_action( 'bricks/form/custom_action', 'my_form_custom_action', 10, 1 );

Seems Pabbly is getting the information. However, I would like the php experts to see if this is indeed the correct way to do so, or if this is an incredibly ugly hack that will explode and burn my dog.

Hello @Sergio

WordPress has a built-in function to post data to a remote URL: wp_remote_post.

You only need to set the $args['body'] = $fields; before calling the $response = wp_remote_post($webhook_url, $args);

I don’t know if you need to manipulate the $fields a little bit to match Paddly data requirements.

Obrigado hermano!

Thanks for this. I will try to study more wordpress functions to really get into wordpress programming. I am still suck in the 2000’s with C++ from university days.

Update: had to add jsonencoding to make it work:

$webhook_url = 'https://connect.pabbly.com/workflow/sendwebhookdata/XXXXX';
  $jsonDataEncoded = json_encode($fields);
  $args['body'] = $fields;
  $response = wp_remote_post($webhook_url, $args);

So this is literally the only code snippet you need?

Just add your webhook URL?

Also, where do you put this code snippet exactly?

Thanks! :raised_hands:

@crsmoore
Yes, that’s pretty much all you need: the webhook url.

I have WPCodebox to put all my code snippets, but I’m sure if you are using the child theme you can edit the function php file to add this.

1 Like

I ended up using this (also in WPCodeBox), executing it only in the “Admin Area” (not the Frontend). It’s working great.

<?php
function my_form_custom_action( $form ) {
   
  $fields = $form->get_fields();
  // $formId = $fields['formId'];
  // $postId = $fields['postId'];
  // $settings = $form->get_settings();
  // $files = $form->get_uploaded_files();
 
  // Perform some logic here...
  $webhook_url = 'https://YOUR-WEBHOOK-URL-GOES-HERE';
 
  $args['body'] = $fields;
  $response = wp_remote_post($webhook_url, $args);

  // Set result in case it fails (if needed)
  $form->set_result([
    'action' => 'my_custom_action',
    'type'    => 'success', //or danger or info
    'message' => esc_html__('Oh my custom action failed', 'bricks'),
  ]);
}

add_action( 'bricks/form/custom_action', 'my_form_custom_action', 10, 1 );
2 Likes

Im lost. Can someone walk through connecting subscribing newsletter form in bricks form to pabbly? Do i need to use “custom” in bricks action? What do i use then in the code used above to match that “custom”?

Yes, you need to choose “custom” as the action and then in your code snippet plugin of choice paste in that code.

Pabbly gives you their webhook url, so paste that pabbly webhook url into the $webhook_url = “https://yourwebhookurl”; line and test to see if it picks up data when you send stuff.

You can setup multiple if statements within the same function.

I touch on this in the video below :point_down:

1 Like

This post having more than 1k views is a sign that people need this webhook feature with the Bricks Form element.

I have been searching for how to use webhook with the Bricks Builder form element.

1 Like

I just went through Bricksforge, I bet they took the addon project personally. The features are too crazy.

1 Like