To integrate WP Forms with Slack, you can use the wp_remote_post() function to send form submissions to a Slack channel. This allows you to receive real-time notifications in Slack whenever a form is submitted on your WordPress site.
Here’s an example code snippet that demonstrates how to achieve this integration:
function wpsnippets_wpforms_slack_integration( $fields, $entry, $form_data ) {
// Define the Slack webhook URL
$slack_webhook_url = 'https://hooks.slack.com/services/your-webhook-url';
// Prepare the Slack message payload
$message = array(
'text' => 'New form submission received:',
'attachments' => array(
array(
'fallback' => 'New form submission received',
'color' => '#36a64f',
'fields' => array(),
),
),
);
// Add form field values to the Slack message payload
foreach ( $fields as $field ) {
$message['attachments'][0]['fields'][] = array(
'title' => $field['label'],
'value' => $field['value'],
'short' => true,
);
}
// Send the Slack message
wp_remote_post( $slack_webhook_url, array(
'body' => wp_json_encode( $message ),
'headers' => array( 'Content-Type' => 'application/json' ),
) );
}
add_action( 'wpforms_process_complete', 'wpsnippets_wpforms_slack_integration', 10, 3 );
In this code snippet, we define the wpsnippets_wpforms_slack_integration() function and hook it to the wpforms_process_complete action. This action is triggered after a form submission is processed by WP Forms.
Inside the function, we first define the Slack webhook URL, which is the endpoint where the form submission data will be sent. Replace 'https://hooks.slack.com/services/your-webhook-url' with your actual Slack webhook URL.
Next, we prepare the Slack message payload using an array. The payload includes a text message and an attachments array, which allows us to format the message nicely in Slack. You can customize the message text and appearance as needed.
We then loop through the form fields and add them to the Slack message payload as attachments. Each field is added as a separate field in the attachments array, with the field label as the title and the field value as the value.
Finally, we use the wp_remote_post() function to send the Slack message. The message payload is encoded as JSON and sent as the request body. We also set the Content-Type header to application/json to indicate that we are sending JSON data.
With this code snippet in place, whenever a form is submitted, the form field values will be sent to your specified Slack channel in real-time. This can be useful for staying updated on form submissions and taking immediate action if needed.
Examples
Example 1: Sending WP Forms submissions to a Slack channel
This use case demonstrates how to integrate WP Forms with Slack by sending form submissions to a specific Slack channel. The code example below uses the wpforms_process_complete action hook to trigger a custom function that sends the form data to Slack using the wp_remote_post() function.
function wpsnippets_send_wpforms_to_slack( $entry, $form_data ) {
$slack_webhook_url = 'https://hooks.slack.com/services/your-webhook-url';
$message = "New form submission:n";
foreach ( $form_data['fields'] as $field ) {
$message .= $field['label'] . ': ' . $field['value'] . "n";
}
$response = wp_remote_post( $slack_webhook_url, array(
'body' => json_encode( array( 'text' => $message ) ),
) );
}
add_action( 'wpforms_process_complete', 'wpsnippets_send_wpforms_to_slack', 10, 2 );
The wpsnippets_send_wpforms_to_slack() function retrieves the Slack webhook URL and constructs a message with the form submission data. It then uses wp_remote_post() to send a POST request to the Slack webhook URL, passing the message as the request body.
Example 2: Customizing the Slack message format
This use case demonstrates how to customize the format of the Slack message sent when a WP Forms submission is received. The code example below modifies the wpsnippets_send_wpforms_to_slack() function from the previous example to include additional information in the Slack message.
function wpsnippets_send_wpforms_to_slack( $entry, $form_data ) {
$slack_webhook_url = 'https://hooks.slack.com/services/your-webhook-url';
$message = "New form submission:n";
$message .= "Form ID: " . $form_data['id'] . "n";
$message .= "Submitted by: " . $entry['ip'] . "n";
foreach ( $form_data['fields'] as $field ) {
$message .= $field['label'] . ': ' . $field['value'] . "n";
}
$response = wp_remote_post( $slack_webhook_url, array(
'body' => json_encode( array( 'text' => $message ) ),
) );
}
add_action( 'wpforms_process_complete', 'wpsnippets_send_wpforms_to_slack', 10, 2 );
In this example, the Slack message includes additional details such as the form ID and the IP address of the submitter. These details are appended to the message before the form field data.
Example 3: Sending form attachments to Slack
This use case demonstrates how to send form attachments, such as uploaded files, to Slack along with the form submission. The code example below extends the previous wpsnippets_send_wpforms_to_slack() function to include attachments in the Slack message.
function wpsnippets_send_wpforms_to_slack( $entry, $form_data ) {
$slack_webhook_url = 'https://hooks.slack.com/services/your-webhook-url';
$message = "New form submission:n";
foreach ( $form_data['fields'] as $field ) {
if ( $field['type'] === 'fileupload' && ! empty( $field['value'] ) ) {
$attachment = wp_get_attachment_url( $field['value'] );
$message .= $field['label'] . ': ' . $attachment . "n";
} else {
$message .= $field['label'] . ': ' . $field['value'] . "n";
}
}
$response = wp_remote_post( $slack_webhook_url, array(
'body' => json_encode( array( 'text' => $message ) ),
) );
}
add_action( 'wpforms_process_complete', 'wpsnippets_send_wpforms_to_slack', 10, 2 );
In this example, the function checks if a form field is of type “fileupload” and if it has a value. If so, it retrieves the attachment URL using wp_get_attachment_url() and includes it in the Slack message. This allows you to easily view and download the uploaded file directly from Slack.
