How to Send the Entry ID Through Webhooks

Introduction

Would you like to pass the entry ID through Webhooks addon? The entry ID isn’t assigned until the form is submitted and is very useful to tie that connection between your WPForms entries and the external CRM you are posting to for cross-checking. Using a small PHP snippet, we’ll show you how to achieve this!

Creating the form

To begin, we’re going to start by creating a form and adding our fields. If you need any assistance with how to create a form, please check out this documentation.

create the form and add your fields.

Setting up the webhook

If you need assistance with setting up the Webhooks addon, please check out this article.

To enable the Webhooks addon, from inside the form builder, navigate to Settings. Next, you click the Enable Webhooks toggle and you can begin to fill in your webhook URL, and then under the Request Body, which is the information you’ll send in the webhook, you can map over the fields from the form.

map your form fields to your variable names and enter the webhook URL.

For the purpose of this tutorial, we’re using the Webhook.site for testing that our information is being sent through our webhook. When you’re setting up your webhook, you would use the webhook URL to the external source you’re posting to and assign your variables. It’s important to remember that each external source that accepts incoming webhooks may have different ways of setting up variables to send the information. You’ll need to research this external source to make sure you don’t need to manually create your variables inside that source to make sure the variable names match what you’re assigning in the form builder.

Adding the snippet

Just copy and paste this snippet to your site.

For any assistance on how and where to add snippets to your site, please see this tutorial.

/**
 * Send the entry id through webhooks request.
 *
 * @link https://wpforms.com/developers/how-to-send-field-values-with-webhooks/
 */

function wpf_dev_webhooks_process_delivery_request_options( $options, $webhook_data, $fields, $form_data, $entry_id ) {
    
	// Optional, you can limit to specific forms. Below, we restrict output to
    // form #1899.
    if ( absint( $form_data[ 'id' ] ) !== 1899 ) {
        return $options;
    }
	
	$body = ! is_array( $options[ 'body' ] ) ? json_decode( $options[ 'body' ], true ) : $options[ 'body' ];
	
	// Create a new entry and assign the entry_id
	$body[ 'entry_id' ] = $entry_id;
	
	// Format request data.
	if ( !empty( $options[ 'method' ] ) && $options[ 'method' ] !== 'GET' && $webhook_data[ 'format' ] === 'json' ) {
		
		// Encode request body.
		$options[ 'body' ] = wp_json_encode( $body );
	}
	
	return $options;
}

add_filter( 'wpforms_webhooks_process_delivery_request_options', 'wpf_dev_webhooks_process_delivery_request_options', 100, 5);

With this snippet, we’re going to add an additional variable to the webhook called entry_id and assign this ID number that is created from WPForms when the form is submitted.

Now when you see the information sent from the webhook, you’ll notice not only your mapped field names are there, but also the entry ID as well.

you can now send the entry ID through webhooks using this snippet

And that’s it! You can now pass the entry ID through your webhook. Would you like to have autofocus on your form when the page loads? Check out our article on How to Add Autofocus on Your Form.