Replace the first zero in Twilio phone numbers with a country code

This snippet will replace the first zero in phone numbers sent to Twilio API with a given country code.

Add the following code snippet towards the end of your child themes functions.php

add_filter( 'automator_twilio_api_call', function( $params ) {

	$replace_first_zero_with = '+42'; // Enter your country code here

	if ( empty( $params['body']['to'] ) ) {
		return $params;
	}

	$to_number = $params['body']['to']; 

	if ( substr( $to_number, 0, 1 ) == "0" ) {
		$to_number = $replace_first_zero_with . substr( $to_number, 1 );
	}

	$params['body']['to'] = $to_number; 

	return $params;
} );