Post Gravity Forms Entries to Slack
Sends a short summary of each new Gravity Forms entry to a Slack channel with a link to the entry, so your team can respond to leads quickly.
Opens WPIDE on
Code
add_action( 'gform_after_submission', function ( $entry, $form ) {
$url = defined( 'WPIDE_SLACK_WEBHOOK_URL' ) ? (string) WPIDE_SLACK_WEBHOOK_URL : '';
if ( 0 !== strpos( $url, 'https://hooks.slack.com/' ) || 'spam' === rgar( $entry, 'status' ) ) {
return;
}
$escape = function ( $text ) {
return str_replace( array( '&', '<', '>' ), array( '&', '<', '>' ), (string) $text );
};
$lines = array( sprintf( '*New entry in "%s"*', $escape( $form['title'] ) ) );
foreach ( $form['fields'] as $field ) {
$value = trim( wp_strip_all_tags( (string) $field->get_value_export( $entry ) ) );
if ( '' !== $value && count( $lines ) < 6 ) {
$lines[] = sprintf( '*%s:* %s', $escape( $field->label ), $escape( wp_html_excerpt( $value, 300, '…' ) ) );
}
}
$lines[] = '<' . admin_url( 'admin.php?page=gf_entries&view=entry&id=' . (int) $form['id'] . '&lid=' . (int) rgar( $entry, 'id' ) ) . '|Open the entry>';
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, 2 );
Notes
Create a Slack incoming webhook and define WPIDE_SLACK_WEBHOOK_URL in wp-config.php; only the first five 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.