Filter uncanny-automator

automator_campaign_monitor_validate_custom_field_value

Filter custom field value. Filters the custom field value before it's saved in Campaign Monitor, allowing for modifications.

add_filter( 'automator_campaign_monitor_validate_custom_field_value', $callback, 10, 4 );

Description

Fires when validating a Campaign Monitor custom field value before subscriber addition/update. Developers can sanitize, modify, or return a WP_Error for invalid values. This hook is useful for enforcing data formats or custom validation rules for specific custom fields.


Usage

add_filter( 'automator_campaign_monitor_validate_custom_field_value', 'your_function_name', 10, 4 );

Parameters

$value (mixed)
- The custom field value, WP_Error or array if multi-select.
$key (string)
- The custom field key.
$original_value (string)
- The original custom field value.
$config (array)
- The custom field config.

Return Value

mixed


Examples

/**
 * Example function to validate a specific custom field for Campaign Monitor.
 * This function checks if a custom field with the key 'user_registration_date'
 * contains a valid date format before it's sent to Campaign Monitor.
 *
 * @param mixed $value          The custom field value.
 * @param string $key           The custom field key.
 * @param string $original_value The original custom field value.
 * @param array $config         The custom field config.
 *
 * @return mixed The validated value or a WP_Error object if validation fails.
 */
function my_automator_validate_registration_date( $value, $key, $original_value, $config ) {
	// Only apply this validation to a specific custom field key.
	if ( 'user_registration_date' === $key ) {

		// Check if the value is empty, which might be acceptable depending on requirements.
		if ( empty( $value ) ) {
			return $value; // Or return a WP_Error if an empty date is not allowed.
		}

		// Attempt to validate the date format.
		// Using DateTime::createFromFormat is a robust way to check date formats.
		$date_format = 'Y-m-d H:i:s'; // Expected format of the registration date.
		$date_obj = DateTime::createFromFormat( $date_format, $value );

		if ( $date_obj && $date_obj->format( $date_format ) === $value ) {
			// The date is valid and matches the expected format.
			return $value;
		} else {
			// The date is not in the expected format. Return a WP_Error.
			return new WP_Error(
				'invalid_registration_date_format',
				__( 'The provided registration date is not in the correct format (YYYY-MM-DD HH:MM:SS).', 'your-text-domain' ),
				array(
					'value' => $value,
					'key'   => $key,
					'config' => $config,
				)
			);
		}
	}

	// If it's not the specific field we're validating, return the original value.
	return $value;
}
add_filter( 'automator_campaign_monitor_validate_custom_field_value', 'my_automator_validate_registration_date', 10, 4 );

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/campaign-monitor/actions/campaign-monitor-add-update-subscriber.php:463

private function validate_custom_field_value( $key, $value, $config ) {

		// Stash original value for filters.
		$original_value = $value;

		// Sanitize value by type.
		$type  = 'select' === $config['type'] && $config['supports_multiple_values'] ? 'multi-select' : $config['type'];
		$value = $this->sanitize_custom_field_value_by_type( $value, $type );

		// Validate value by options.
		if ( ! is_wp_error( $value ) && ! empty( $config['options'] ) ) {
			$value = $this->validate_custom_field_value_by_options( $value, $config['options'] );
		}

		/**
		 * Filter custom field value.
		 *
		 * @param mixed $value - The custom field value, WP_Error or array if multi-select.
		 * @param string $key - The custom field key.
		 * @param string $original_value - The original custom field value.
		 * @param array $config - The custom field config.
		 *
		 * @return mixed
		 */
		$value = apply_filters( 'automator_campaign_monitor_validate_custom_field_value', $value, $key, $original_value, $config );

		return $value;
	}

Scroll to Top