Filter uncanny-automator

automator_active_campaign_add_contact_api_body

Filters the API request body for adding a contact to ActiveCampaign, allowing modification before it's sent.

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

Description

Filters the API request body before adding a contact to ActiveCampaign. Developers can modify the contact data, such as adding custom fields or updating existing ones, by returning a modified `$body` array. This hook fires just before the API request is made, allowing for dynamic data manipulation.


Usage

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

Parameters

$body (mixed)
This parameter contains the preliminary API request body intended for adding a contact to ActiveCampaign.
$args (mixed)
This parameter contains the preliminary data for the ActiveCampaign API contact object, which can be filtered and modified before being sent.

Return Value

The filtered value.


Examples

/**
 * Example of how to modify the API body for adding a contact to ActiveCampaign.
 * This function adds a custom field to the contact's data.
 *
 * @param array $body The current API request body.
 * @param array $args Additional arguments passed to the filter.
 * @return array The modified API request body.
 */
function my_automator_modify_active_campaign_contact_body( $body, $args ) {
    // Ensure the required 'email' key exists in the body
    if ( ! isset( $body['email'] ) || empty( $body['email'] ) ) {
        // You might want to log an error or handle this case appropriately
        return $body;
    }

    // Assume $args contains a 'user_id' from the WordPress user initiating the action
    // and we want to map that to a custom field in ActiveCampaign.
    if ( isset( $args['user_id'] ) && ! empty( $args['user_id'] ) ) {
        // Assuming 'wordpress_user_id' is a custom field key in ActiveCampaign
        // The structure for custom fields in ActiveCampaign API can vary,
        // but often it's an array of field/value pairs.
        // For simplicity, let's assume the 'fields' key is directly in the body
        // and it's an array of key-value pairs.
        $body['fields'] = $body['fields'] ?? []; // Initialize if not set
        $body['fields']['wordpress_user_id'] = $args['user_id'];
    }

    // You can add more logic here to conditionally add or modify other fields
    // based on the $body or $args.

    return $body;
}
add_filter( 'automator_active_campaign_add_contact_api_body', 'my_automator_modify_active_campaign_contact_body', 10, 2 );

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/active-campaign/helpers/active-campaign-app-helpers.php:569

public function filter_add_contact_api_body( $body, $args ) {

		$body = apply_filters( 'automator_active_campaign_add_contact_api_body', $body, $args );

		// Build the contact object
		$contact = array(
			'email' => $body['email'],
		);

		// Fields that should be included in contact object
		$contact_fields = array( 'firstName', 'lastName', 'phone' );

		foreach ( $contact_fields as $field ) {
			if ( ! isset( $body[ $field ] ) ) {
				continue;
			}

			$value = $body[ $field ];

			// Handle [DELETE] - add as empty to actively remove
			if ( '[delete]' === trim( strtolower( $value ) ) ) {
				$contact[ $field ] = '';
			} elseif ( ! empty( trim( $value ) ) ) {
				// Handle actual values - only add if not empty
				$contact[ $field ] = $value;
			}
		}

		// Process custom fields
		if ( isset( $body['fields'] ) && is_array( $body['fields'] ) ) {
			$field_values = array();
			foreach ( $body['fields'] as $field ) {
				if ( '[delete]' === trim( $field['value'] ) ) {
					$field['value'] = '';
				}
				$field_values[] = $field;
			}
			if ( ! empty( $field_values ) ) {
				$contact['fieldValues'] = $field_values;
			}
		}

		// Build the final body
		return array(
			'action'         => $body['action'],
			'email'          => $body['email'],
			'contact'        => wp_json_encode(
				array(
					'contact' => $contact,
				)
			),
			'updateIfExists' => $body['updateIfExists'] ?? false,
		);
	}

Scroll to Top