To integrate WP Forms with Zapier, you can use the wpforms_zapier_integration
hook provided by the WP Forms plugin. This hook allows you to send form submissions to Zapier, which can then be used to trigger various actions and automations.
Here’s an example code snippet that demonstrates how to use the wpforms_zapier_integration
hook to send form submissions to Zapier:
function wpsnippets_wpforms_zapier_integration( $fields, $entry, $form_data ) {
// Prepare the data to be sent to Zapier
$data = array(
'name' => $entry['fields'][0]['value'],
'email' => $entry['fields'][1]['value'],
'message' => $entry['fields'][2]['value'],
);
// Convert the data to JSON format
$json_data = json_encode( $data );
// Send the data to Zapier using a POST request
$response = wp_remote_post( 'https://hooks.zapier.com/hooks/catch/1234567/abcdefg/', array(
'body' => $json_data,
) );
// Check if the request was successful
if ( ! is_wp_error( $response ) && wp_remote_retrieve_response_code( $response ) === 200 ) {
// Log a success message
error_log( 'Form submission sent to Zapier successfully.' );
} else {
// Log an error message
error_log( 'Failed to send form submission to Zapier.' );
}
}
add_action( 'wpforms_zapier_integration', 'wpsnippets_wpforms_zapier_integration', 10, 3 );
In this example, the wpsnippets_wpforms_zapier_integration
function is hooked to the wpforms_zapier_integration
action. It receives three parameters: $fields
, $entry
, and $form_data
. These parameters contain the form field values, entry data, and form data respectively.
Inside the function, we prepare the data to be sent to Zapier by extracting the relevant field values from the $entry
parameter. We then convert the data to JSON format using
on_encode
Next, we use the wp_remote_post
function to send a POST request to the Zapier webhook URL. The $json_data
is passed as the request body. The response from Zapier is stored in the $response
variable.
We check if the request was successful by verifying that the response code is 200. If successful, we log a success message using error_log
. Otherwise, we log an error message.
This code snippet can be useful when you want to integrate WP Forms with Zapier to automate actions based on form submissions. For example, you can use Zapier to automatically create a new lead in your CRM system whenever a form is submitted on your WordPress site.
Examples
Example 1: Sending form submissions to Zapier
This use case demonstrates how to integrate WP Forms with Zapier to send form submissions to external services or applications. The code example below shows how to use the wpforms_process_complete
hook to send form data to Zapier.
add_action( 'wpforms_process_complete', 'wpsnippets_send_form_data_to_zapier', 10, 4 );
function wpsnippets_send_form_data_to_zapier( $fields, $entry, $form_data, $entry_id ) {
// Prepare the data to be sent to Zapier
$data = array(
'name' => $fields['name'],
'email' => $fields['email'],
'message' => $fields['message'],
);
// Send the data to Zapier using a webhook
$response = wp_remote_post( 'https://hooks.zapier.com/hooks/catch/123456/abcdef/', array(
'body' => json_encode( $data ),
'headers' => array( 'Content-Type' => 'application/json' ),
) );
}
In this code example, we use the wpforms_process_complete
hook to trigger the function wpsnippets_send_form_data_to_zapier
after a form submission is processed. Inside the function, we prepare the form data to be sent to Zapier by extracting the relevant fields from the $fields
array. We then use the wp_remote_post
function to send the data to Zapier using a webhook.
Example 2: Adding additional form data to Zapier payload
This use case demonstrates how to include additional data along with the form submission when sending it to Zapier. The code example below shows how to modify the data array to include custom fields.
add_action( 'wpforms_process_complete', 'wpsnippets_send_form_data_to_zapier', 10, 4 );
function wpsnippets_send_form_data_to_zapier( $fields, $entry, $form_data, $entry_id ) {
// Prepare the data to be sent to Zapier
$data = array(
'name' => $fields['name'],
'email' => $fields['email'],
'message' => $fields['message'],
'custom_field' => 'Custom value',
);
// Send the data to Zapier using a webhook
$response = wp_remote_post( 'https://hooks.zapier.com/hooks/catch/123456/abcdef/', array(
'body' => json_encode( $data ),
'headers' => array( 'Content-Type' => 'application/json' ),
) );
}
In this code example, we modify the $data
array to include an additional custom field called 'custom_field'
with a static value of 'Custom value'
. This allows you to send extra information along with the form submission to Zapier.
Example 3: Conditionally sending form data to Zapier
This use case demonstrates how to conditionally send form data to Zapier based on certain criteria. The code example below shows how to check the value of a specific field and only send the data if the condition is met.
add_action( 'wpforms_process_complete', 'wpsnippets_send_form_data_to_zapier', 10, 4 );
function wpsnippets_send_form_data_to_zapier( $fields, $entry, $form_data, $entry_id ) {
// Check if a specific field has a certain value
if ( $fields['specific_field'] === 'specific_value' ) {
// Prepare the data to be sent to Zapier
$data = array(
'name' => $fields['name'],
'email' => $fields['email'],
'message' => $fields['message'],
);
// Send the data to Zapier using a webhook
$response = wp_remote_post( 'https://hooks.zapier.com/hooks/catch/123456/abcdef/', array(
'body' => json_encode( $data ),
'headers' => array( 'Content-Type' => 'application/json' ),
) );
}
}
In this code example, we check the value of a specific field called 'specific_field'
using an if
statement. If the field value matches 'specific_value'
, we proceed to prepare and send the form data to Zapier as shown in the previous examples. This allows you to selectively send form submissions to Zapier based on certain conditions.