Post WPForms Submissions to Slack
Sends a summary of each WPForms submission to a Slack channel through an incoming webhook, so the team sees new enquiries straight away.
Opens WPIDE on
Code
add_action( 'wpforms_process_complete', function ( $fields, $entry, $form_data ) {
$url = defined( 'WPIDE_SLACK_WEBHOOK_URL' ) ? (string) WPIDE_SLACK_WEBHOOK_URL : '';
if ( 0 !== strpos( $url, 'https://hooks.slack.com/' ) ) {
return;
}
$escape = function ( $text ) {
return str_replace( array( '&', '<', '>' ), array( '&', '<', '>' ), (string) $text );
};
$title = isset( $form_data['settings']['form_title'] ) ? $form_data['settings']['form_title'] : 'WPForms';
$lines = array( sprintf( '*New submission: %s*', $escape( $title ) ) );
foreach ( (array) $fields as $field ) {
$value = isset( $field['value'] ) ? trim( wp_strip_all_tags( (string) $field['value'] ) ) : '';
if ( '' !== $value && count( $lines ) < 7 ) {
$lines[] = sprintf( '*%s:* %s', $escape( $field['name'] ), $escape( wp_html_excerpt( $value, 300, '…' ) ) );
}
}
wp_safe_remote_post( $url, array(
'body' => wp_json_encode( array( 'text' => implode( "\n", $lines ) ) ),
'headers' => array( 'Content-Type' => 'application/json' ),
'timeout' => 5,
'blocking' => false,
) );
}, 10, 3 );
Notes
Define WPIDE_SLACK_WEBHOOK_URL in wp-config.php; only the first six filled fields are included.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.