Filter uncanny-automator

uncanny_automator_ontraport_fields

Filters Ontraport field data before it's sent to the API, allowing modification of field values.

add_filter( 'uncanny_automator_ontraport_fields', $callback, 10, 2 );

Description

Fires before sending data to Ontraport to upsert a contact. Developers can use this filter to modify the data being sent, such as adding or altering fields. The `$body` parameter contains the contact data, and `$action_data` provides context about the Uncanny Automator action.


Usage

add_filter( 'uncanny_automator_ontraport_fields', 'your_function_name', 10, 2 );

Parameters

$body (mixed)
This parameter likely holds the raw body of the request or response from Ontraport, but its specific contents are not fully defined by the provided snippet.
$action_data (mixed)
This parameter is a mixed value that represents the body of the API request to Ontraport.

Return Value

The filtered value.


Examples

/**
 * Example of how to filter the fields sent to Ontraport for contact upsert.
 * This example demonstrates adding a custom field or modifying an existing one.
 */
add_filter(
	'uncanny_automator_ontraport_fields',
	function( $body, $action_data ) {
		// Ensure we are dealing with the correct action type if needed,
		// though the hook itself is specific to Ontraport.
		// $action_type = isset( $action_data['action_type'] ) ? $action_data['action_type'] : '';

		// Decode the existing JSON encoded fields to work with them.
		$decoded_fields = json_decode( $body['fields'], true );

		// Example: Add a new custom field if it doesn't exist.
		// Replace 'My Custom Field Name' with the actual Ontraport field name and 'New Value' with your desired value.
		// You might dynamically generate this value based on $action_data or other WordPress data.
		if ( ! isset( $decoded_fields['my_custom_ontraport_field'] ) ) {
			$decoded_fields['my_custom_ontraport_field'] = 'Value from WordPress';
		}

		// Example: Modify an existing field.
		// Let's say we want to ensure a specific field is always set to a certain value.
		if ( isset( $decoded_fields['email'] ) ) {
			// You might want to sanitize or validate the email here before passing it on.
			// $decoded_fields['email'] = sanitize_email( $decoded_fields['email'] );
		}

		// Re-encode the fields array back into JSON format for the 'fields' parameter.
		$body['fields'] = wp_json_encode( $decoded_fields );

		// Return the modified $body array.
		return $body;
	},
	10, // Priority
	2  // Accepted arguments count
);

Placement

This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.


Source Code

src/integrations/ontraport/actions/ontraport-upsert-contact.php:83

protected function process_action( $user_id, $action_data, $recipe_id, $args, $parsed ) {

		$email = $this->helpers->validate_email( $this->get_parsed_meta_value( $this->get_action_meta(), '' ) );

		$fields = array( 'email' => $email );

		foreach ( $this->get_contact_fields() as $field ) {
			$fields[ $field['api_key'] ] = $this->get_parsed_meta_value( $field['option_code'], '' );
		}

		$fields['status'] = $this->get_parsed_meta_value( 'STATUS', '' );

		$body = array(
			'fields' => wp_json_encode( $fields ),
		);

		$body = apply_filters(
			'uncanny_automator_ontraport_fields',
			$body,
			array(
				$action_data,
				$recipe_id,
				$args,
				$parsed,
			)
		);

		$this->api->send_request( 'contact_upsert', $body, $action_data );

		return true;
	}


Scroll to Top