Filter
uncanny-automator
automator_hubspot_add_contact_properties
Filters properties for adding a HubSpot contact and fires before the contact is created.
add_filter( 'automator_hubspot_add_contact_properties', $callback, 10, 2 );
Description
Filters the contact properties sent to HubSpot during contact creation. Developers can add, modify, or remove properties before they are submitted. This hook is applied after default and custom fields are gathered, allowing for dynamic manipulation of the data.
Usage
add_filter( 'automator_hubspot_add_contact_properties', 'your_function_name', 10, 2 );
Parameters
-
$properties(mixed) - This parameter contains an array of properties to be added or updated for a HubSpot contact.
-
$user_id(mixed) - This parameter contains an array of contact properties that will be added or updated in HubSpot.
Return Value
The filtered value.
Examples
<?php
/**
* Example of how to filter Hubspot contact properties before they are added.
* This function might add a custom property based on the user's role.
*
* @param array $properties The current array of contact properties.
* @param array $args An array containing user_id, action_data, recipe_id, and args.
*
* @return array The modified array of contact properties.
*/
add_filter(
'automator_hubspot_add_contact_properties',
function ( $properties, $args ) {
$user_id = isset( $args['user_id'] ) ? absint( $args['user_id'] ) : 0;
if ( $user_id > 0 ) {
$user = get_user_by( 'id', $user_id );
if ( $user && in_array( 'administrator', (array) $user->roles ) ) {
// Add a custom property if the user is an administrator.
$properties['is_administrator'] = array(
'value' => true,
'property_type' => 'boolean' // Assuming Hubspot property type
);
}
// Example of adding another property based on a custom field.
// Replace 'your_custom_field_key' with an actual user meta key.
$custom_user_meta = get_user_meta( $user_id, 'your_custom_field_key', true );
if ( ! empty( $custom_user_meta ) ) {
$properties['custom_data_from_user'] = array(
'value' => sanitize_text_field( $custom_user_meta ),
'property_type' => 'string' // Assuming Hubspot property type
);
}
}
return $properties;
},
10, // Priority
2 // Accepted arguments
);
?>
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-createcontact.php:90
src/integrations/hubspot/actions/hubspot-create-contact.php:107
protected function process_action( $user_id, $action_data, $recipe_id, $args, $parsed ) {
$email = trim( $this->get_parsed_meta_value( 'HUBSPOTEMAIL', '' ) );
$update = true;
if ( ! empty( $action_data['meta']['UPDATE'] ) ) {
$update = filter_var( $action_data['meta']['UPDATE'], FILTER_VALIDATE_BOOLEAN );
}
$properties = array(
array(
'property' => 'email',
'value' => $email,
),
);
// 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_contact_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;
}