gform_webhooks_request_data

Description

This filter allows the webhook HTTP request data to be modified.

Usage

The following would apply to all forms:

add_filter( 'gform_webhooks_request_data', 'your_function_name', 10, 4 )

To target a specific form, append the form id to the hook name. (format: gform_webhooks_request_data_FORMID)

add_filter( 'gform_webhooks_request_data_7', 'your_function_name', 10, 4 )

Parameters

Examples

1. Add a new field

add_filter( 'gform_webhooks_request_data', 'modify_data', 10, 4 );
function modify_data( $request_data, $feed, $entry, $form ){
  $request_data[1] = 'Tester McTesty';
  return $request_data;
}

2. Modify the field keys

This example shows how the field/input IDs, which are used as the keys to the data when the “all fields” choice is selected for the request body setting, can be replaced. In this case the period used in the input IDs is being replaced with an underscore so the webhook to Automate.io will succeed.

add_filter( 'gform_webhooks_request_data', function ( $request_data, $feed ) {
	if ( rgars( $feed, 'meta/requestBodyType' ) === 'all_fields'
	     && strpos( rgars( $feed, 'meta/requestURL' ), 'automate.io' ) !== false
	) {
		foreach ( $request_data as $key => $value ) {
			if ( is_numeric( $key ) ) {
				$request_data[ 'field ' . str_replace( '.', '_', $key ) ] = $value;
				unset( $request_data[ $key ] );
			}
		}
	}

	return $request_data;
}, 10, 2 );

3. Modify the field values for the Events Calendar plugin

This example shows how some field values, which will be strings when mapped to form field values, can be changed to booleans as required by the Events Calendar plugin.

add_filter( 'gform_webhooks_request_data', function ( $request_data, $feed ) {
	$request_url = rgars( $feed, 'meta/requestURL' );
	if ( strpos( $request_url, 'tribe/events/' ) !== false ) {
		$requires_bool_values = array(
			'all_day',
			'show_map',
			'show_map_link',
			'hide_from_listings',
			'sticky',
			'featured',
		);

		foreach ( $request_data as $key => $value ) {
			if ( in_array( $key, $requires_bool_values ) ) {
				$request_data[ $key ] = (bool) $value;
			}
		}
	}

	return $request_data;
}, 10, 2 );

4. Remove empty values

This example shows how empty fields can be removed from the request data when the “all fields” choice is selected for the request body setting.

add_filter( 'gform_webhooks_request_data', function ( $request_data, $feed ) {
	if ( rgars( $feed, 'meta/requestBodyType' ) === 'all_fields' ) {
		foreach ( $request_data as $key => $value ) {
			if ( empty( $value ) ) {
				unset( $request_data[ $key ] );
			}
		}
	}

	return $request_data;
}, 10, 2 );

Placement

This code should be placed in the functions.php file of your active theme.

Source Code

This filter is located in GF_Webhooks::get_request_data() in gravityformswebhooks/class-gf-webhooks.php.