This is a WordPress function that sends form data to a webhook URL using the WordPress HTTP API. It is hooked to the frm_after_create_entry
action, which means that it will be executed after an entry is created in a form. The function takes two arguments: $entry_id
, which is the ID of the entry that was just created, and $form_id
, which is the ID of the form.
The function first retrieves the form data using the FrmEntry::getOne()
function. Then it sets the webhook URL, the headers for the HTTP request, and the data to be sent to the webhook. The data is an array of field values, where the keys are the field names and the values are the form data. The data is then encoded as JSON and sent to the webhook using the wp_remote_post()
function.
This function can be used to send form data from a WordPress site to a service like Airtable, which can be used to store and manage the data.
add_action( 'frm_after_create_entry', 'send_form_data_to_webhook', 20, 2 ); function send_form_data_to_webhook( $entry_id, $form_id ) { // Get the form data $form_data = FrmEntry::getOne( $entry_id, true ); // Set the webhook URL $webhook_url = 'https://hooks.airtable.com/workflows/v1/genericWebhook/appccREHyyEmEDDsv/wflBCXpZw5BeHiUEF/wtrmLFQPfgoTaIvOA'; // Set the headers for the HTTP request $headers = array( 'Content-Type' => 'application/json' ); // Set the data to be sent to the webhook $data = array( 'fields' => array( // Set the field values here, using the form data // For example, if the form has a field with the name "Name", you can set the value like this: 'Name' => $form_data->name, // Add additional fields as needed ) ); // Encode the data as JSON $json_data = json_encode( $data ); // Send the HTTP request to the webhook $response = wp_remote_post( $webhook_url, array( 'method' => 'POST', 'headers' => $headers, 'body' => $json_data ) ); }