wpf_forms_pre_submission_contact_id

#Overview

This filter is run when WP Fusion is processing a form submission from one of our supported form plugins.

It is triggered after WP Fusion has attempted to locate a contact ID in the CRM for the form submission, but before a contact record is created / updated, and before any tags are applied.

To use the code examples below, add them to your active theme’s functions.php file.

#Alternate filters

For more precise targeting there are two alternate filters with the same arguments:

  • wpf_{integration slug}_pre_submission_contact_id : Where {integration_slug} is the name of the form integration, for example wpf_gform_pre_submission_contact_id

#Parameters

  • $contact_id (string|false): The contact ID in the CRM to be updated, or false if no match found
  • $update_data (array): The data about to be synced to the CRM, in key => value pairs
  • $user_id (int): The ID of the registered user who submitted the form, or 0 for guest
  • $form_id (int): The ID of the submitted form

#Examples

#Force all form submissions to create a new contact record

This is a simple example that stops WP Fusion from trying update existing contact records in the CRM from form submissions. All form submissions will create a new contact.

add_filter( 'wpf_forms_pre_submission_contact_id', '__return_false' );

#Force all form submissions to create a new contact record by form ID

This is the same as the example above but only runs on form IDs 22 and 29

function always_create_new_contacts( $contact_id, $update_data, $user_id, $form_id ) {

	if ( 22 == $form_id || 29 == $form_id ) {
		$contact_id = false;
	}

	return $contact_id;

}

add_filter( 'wpf_forms_pre_submission_contact_id', 'always_create_new_contacts', 10, 4 );

#Use custom lookup logic for contact records – Infusionsoft

By default WP Fusion uses the first email address found on a submitted form when determining whether to create or update a contact record in your CRM.

In some cases you may want to use more complex rules for determining when a new record should be created.

This example for Infusionsoft attempts to match against the Email Address 2 and Email Address 3 fields in addition to the Email Address field. That means that if the form is submitted and the entered email matches the Email Address 3 field on an existing contact, that contact record will be updated (rather than a new contact record being added).

function wpf_lookup_additional_emails( $contact_id, $update_data, $user_id, $form_id ) {

	if ( empty( $contact_id ) ) {

		$email = $update_data['Email'];

		wp_fusion()->crm->connect();

		$query  = array( 'EmailAddress2' => $email );
		$result = wp_fusion()->crm->app->dsQuery( 'Contact', 1, 0, $query, array( 'Id' ) );

		if ( isset( $result[0]['Id'] ) ) {

			return $result[0]['Id'];

		}

		$query  = array( 'EmailAddress3' => $email );
		$result = wp_fusion()->crm->app->dsQuery( 'Contact', 1, 0, $query, array( 'Id' ) );

		if ( isset( $result[0]['Id'] ) ) {

			return $result[0]['Id'];

		}
	}

	return $contact_id;

}

add_filter( 'wpf_forms_pre_submission_contact_id', 'wpf_lookup_additional_emails', 10, 4 );

#Use custom lookup logic for contact records – Salesforce

This example for Salesforce only updates an existing contact record if the email address, first name, and last name match. Otherwise a new record will be created.

function wpf_forms_name_lookup( $contact_id, $update_data, $user_id, $form_id ) {

	$params = wp_fusion()->crm->get_params(); // get the authentication headers and other API params

	// URL-encode the three lookup fields

	$email_address = urlencode( $update_data['Email'] );
	$first_name    = urlencode( $update_data['FirstName'] );
	$last_name     = urlencode( $update_data['LastName'] );

	// this is the SOQL, Salesforce Object Query Language:

	$query_args = array(
		'q' => "SELECT Id from {wp_fusion()->crm->object_type} WHERE Email = '{$email_address}' AND FirstName = '{$first_name}' AND LastName = '{$last_name}'",
	);

	$request  = add_query_arg( $query_args, wp_fusion()->crm->instance_url . '/services/data/v42.0/query' );
	$response = wp_remote_get( $request, $params );

	if ( is_wp_error( $response ) ) {

		// If an error was encountered, log it
		wpf_log( 'error', $user_id, 'Error looking up Salesforce contact: ' . $response->get_error_message() );
		return false;

	}

	$response = json_decode( wp_remote_retrieve_body( $response ) );

	if ( empty( $response ) || empty( $response->records ) ) {

		// If no match was found, return false so a new contact is created
		return false;

	}

	// Return the ID of the contact that matched the email, first name, and last name
	// It's this contact ID that will be updated by the form submission

	return $response->records[0]->Id;

}

add_filter( 'wpf_forms_pre_submission_contact_id', 'wpf_forms_name_lookup', 10, 4 );

Was this helpful?