Halloween Sale 15% off Ends in See the plans
PHP

Send Gravity Forms Entries to a Webhook

Posts every new Gravity Forms entry as JSON, with field labels as keys, to a webhook URL such as Zapier, Make, n8n or your CRM, without the paid webhooks add-on.

Don’t have WPIDE yet? Install it on your site

Code

$wpide_gf_webhook_forms = array(); // Form IDs to send; empty = all forms.

add_action( 'gform_after_submission', function ( $entry, $form ) use ( $wpide_gf_webhook_forms ) {
    $url = defined( 'WPIDE_GF_WEBHOOK_URL' ) ? (string) WPIDE_GF_WEBHOOK_URL : '';

    if ( ! in_array( wp_parse_url( $url, PHP_URL_SCHEME ), array( 'https', 'http' ), true ) || 'spam' === rgar( $entry, 'status' ) ) {
        return;
    }
    if ( $wpide_gf_webhook_forms && ! in_array( (int) $form['id'], array_map( 'intval', $wpide_gf_webhook_forms ), true ) ) {
        return;
    }

    $fields = array();
    foreach ( $form['fields'] as $field ) {
        if ( in_array( $field->type, array( 'html', 'section', 'page', 'captcha' ), true ) ) {
            continue;
        }
        $label            = $field->adminLabel ? $field->adminLabel : $field->label;
        $fields[ $label ] = $field->get_value_export( $entry );
    }

    $body = wp_json_encode( array(
        'form_id'    => (int) $form['id'],
        'form_title' => $form['title'],
        'entry_id'   => (int) rgar( $entry, 'id' ),
        'fields'     => $fields,
        'source_url' => rgar( $entry, 'source_url' ),
        'created'    => rgar( $entry, 'date_created' ),
        'site'       => home_url(),
    ) );

    $headers = array( 'Content-Type' => 'application/json' );
    if ( defined( 'WPIDE_GF_WEBHOOK_SECRET' ) && WPIDE_GF_WEBHOOK_SECRET ) {
        $headers['X-Signature'] = hash_hmac( 'sha256', $body, WPIDE_GF_WEBHOOK_SECRET );
    }

    wp_remote_post( $url, array( 'body' => $body, 'headers' => $headers, 'timeout' => 5, 'blocking' => false ) );
}, 10, 2 );

Notes

Define WPIDE_GF_WEBHOOK_URL (and optionally WPIDE_GF_WEBHOOK_SECRET for an X-Signature HMAC header) in wp-config.php; spam entries are never sent.

How to add this snippet

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.