Filter uncanny-automator

automator_hubspot_add_user_properties

Filters the HubSpot user properties before adding or updating a contact in HubSpot.

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

Description

Filters the properties array before adding a user to HubSpot. Developers can use this hook to modify, add, or remove user properties based on the user ID. This is useful for dynamically mapping WordPress user data to HubSpot properties or adding custom data.


Usage

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

Parameters

$properties (mixed)
This parameter contains an array of properties to be added to the HubSpot contact.
$user_id (mixed)
This parameter contains an array of properties that will be sent to HubSpot for the user.

Return Value

The filtered value.


Examples

/**
 * Example of how to modify Hubspot user properties before they are added.
 *
 * This filter allows you to add, remove, or modify properties that will be sent to Hubspot
 * when a new user is created or an existing user is updated via the Automator plugin.
 *
 * @param array $properties An array of properties to be sent to Hubspot.
 * @param array $context    An array containing contextual information:
 *                          'user_id'   => The ID of the WordPress user.
 *                          'action_data' => The data associated with the Automator action.
 *                          'recipe_id' => The ID of the Automator recipe.
 *                          'args'      => Additional arguments passed to the action.
 *
 * @return array The modified array of properties.
 */
add_filter(
	'automator_hubspot_add_user_properties',
	function( $properties, $context ) {
		// Ensure the user_id is available and is a valid integer.
		if ( ! isset( $context['user_id'] ) || ! is_numeric( $context['user_id'] ) ) {
			// If user ID is missing or invalid, return properties as is.
			return $properties;
		}

		$user_id = (int) $user_id;
		$user    = get_user_by( 'id', $user_id );

		// Check if the user object could be retrieved.
		if ( ! $user instanceof WP_User ) {
			// If user not found, return properties as is.
			return $properties;
		}

		// Example: Add a custom Hubspot property based on user role.
		$user_roles = $user->roles;
		if ( in_array( 'administrator', $user_roles, true ) ) {
			$properties['role'] = array(
				'value' => 'WordPress Administrator',
			);
		} elseif ( in_array( 'editor', $user_roles, true ) ) {
			$properties['role'] = array(
				'value' => 'WordPress Editor',
			);
		} else {
			$properties['role'] = array(
				'value' => 'WordPress Subscriber',
			);
		}

		// Example: Add the user's signup date as a Hubspot property.
		$properties['signup_date'] = array(
			'value' => date( 'Y-m-d H:i:s', strtotime( $user->user_registered ) ),
		);

		// Example: Remove a property if it's not needed.
		if ( isset( $properties['some_unwanted_property'] ) ) {
			unset( $properties['some_unwanted_property'] );
		}

		// Example: Modify an existing property if present.
		if ( isset( $properties['email'] ) && isset( $properties['email']['value'] ) ) {
			// Potentially append something to the email, though usually not recommended.
			// $properties['email']['value'] = $properties['email']['value'] . '_modified';
		}

		// Return the modified properties array.
		return $properties;
	},
	10, // Priority
	2  // Accepted arguments: $properties, $context
);

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/hubspot/deprecated/hubspot-adduser.php:100
src/integrations/hubspot/actions/hubspot-add-user.php:113

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

		$user = get_userdata( $user_id );
		if ( ! $user ) {
			throw new Exception( esc_html_x( 'User not found.', 'HubSpot', 'uncanny-automator' ) );
		}

		$update = true;
		if ( ! empty( $action_data['meta']['UPDATE'] ) ) {
			$update = filter_var( $action_data['meta']['UPDATE'], FILTER_VALIDATE_BOOLEAN );
		}

		$properties = array(
			array(
				'property' => 'email',
				'value'    => $user->user_email,
			),
			array(
				'property' => 'firstname',
				'value'    => $user->first_name,
			),
			array(
				'property' => 'lastname',
				'value'    => $user->last_name,
			),
		);

		// Use trait's process_additional_fields (same repeater structure: FIELD_NAME + FIELD_VALUE).
		$custom_fields_json = $this->get_parsed_meta_value( 'CUSTOM_FIELDS', '' );
		$custom_properties  = $this->process_additional_fields( $custom_fields_json );
		$properties         = array_merge( $properties, $custom_properties );

		$properties = apply_filters(
			'automator_hubspot_add_user_properties',
			$properties,
			array(
				'user_id'     => $user_id,
				'action_data' => $action_data,
				'recipe_id'   => $recipe_id,
				'args'        => $args,
			)
		);

		$this->api->create_contact( $properties, $update, $action_data );

		return true;
	}

Scroll to Top