Filter uncanny-automator

automator_campaign_monitor_mobile_number

Filters the mobile number before sending it to Campaign Monitor, allowing for modification.

add_filter( 'automator_campaign_monitor_mobile_number', $callback, 10, 1 );

Description

Fires after a mobile number is retrieved for Campaign Monitor subscribers. Developers can use this filter to sanitize, validate, or modify the mobile number before it's used to add or update a subscriber. Ensure the modified value conforms to E.164 format for proper integration.


Usage

add_filter( 'automator_campaign_monitor_mobile_number', 'your_function_name', 10, 1 );

Parameters

$value (mixed)
This parameter holds the mobile number that will be added or updated for a subscriber in Campaign Monitor.

Return Value

The filtered value.


Examples

/**
 * Filters the mobile number before it's processed by Campaign Monitor.
 *
 * This function provides an opportunity to sanitize or reformat the mobile number.
 * For example, it could be used to enforce a specific international format or
 * remove unwanted characters.
 *
 * @param string $mobile_number The mobile number to filter.
 *
 * @return string The filtered mobile number.
 */
add_filter(
	'automator_campaign_monitor_mobile_number',
	function ( $mobile_number ) {
		// Example: Ensure the mobile number starts with a plus sign.
		// If it doesn't, prepend a default country code (e.g., +1 for US).
		// In a real-world scenario, you might want a more robust country code detection or user input.
		if ( ! str_starts_with( $mobile_number, '+' ) ) {
			// Replace with a more intelligent country code determination or a user-defined default.
			return '+1' . $mobile_number;
		}

		// Example: Remove any spaces or hyphens from the number.
		$mobile_number = str_replace( array( ' ', '-' ), '', $mobile_number );

		return $mobile_number;
	},
	10, // Priority
	1  // 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/campaign-monitor/actions/campaign-monitor-add-update-subscriber.php:307

private function build_conditional_fields( $parsed ) {

		// Map fields.
		$map = array(
			'NAME'          => 'Name',
			'MOBILE_NUMBER' => 'MobileNumber',
		);

		// Consent fields.
		$consent = array(
			'CONSENT_TO_SEND_SMS' => 'ConsentToReceiveSMS',
			'CONSENT_TO_TRACK'    => 'ConsentToTrack',
		);

		// Map bools.
		$bools = array(
			'RESUBSCRIBE'             => 'Resubscribe',
			'RESTART_AUTO_RESPONDERS' => 'RestartSubscriptionBasedAutoresponders',
		);

		// Merge fields.
		$map = array_merge( $map, $consent, $bools );

		// Build data.
		$data = array();
		foreach ( $map as $key => $field ) {

			// Exclude if resubscribe is false.
			if ( 'RESTART_AUTO_RESPONDERS' === $key && empty( $data['Resubscribe'] ) ) {
				continue;
			}

			// Get value.
			$value = key_exists( $key, $bools )
				? $this->get_bool_value( $key )
				: $this->get_parsed_meta_value( $key, '', $parsed );

			// Validate mobile number if provided.
			if ( 'MOBILE_NUMBER' === $key && ! empty( $value ) ) {
				$value = apply_filters( 'automator_campaign_monitor_mobile_number', $value );
				if ( ! $this->validate_is_mobile_E164( $value ) ) {
					$__value = sprintf(
						// translators: 1: Mobile number
						esc_html_x( 'Invalid mobile number format: "%s". Please ensure it begins with a plus sign (+), followed by the country code and phone number.', 'Campaign Monitor', 'uncanny-automator' ),
						$value
					);

					$this->complete_with_notice_messages[] = $__value;
					continue;
				}
			}

			// Exclude sms consent if empty mobile number.
			if ( 'CONSENT_TO_SEND_SMS' === $key && ! key_exists( 'MobileNumber', $data ) ) {
				continue;
			}

			// Add to data.
			if ( ! empty( $value ) ) {
				// Clear value if delete
				$data[ $field ] = self::DELETE_KEY === trim( $value ) ? '' : $value;
			}
		}

		return $data;
	}

Scroll to Top