The code snippet provided below demonstrates how to format a phone number field created using Advanced Custom Fields (ACF) in WordPress. This code snippet can be useful when you want to ensure that the phone number entered by the user is displayed in a consistent format, such as adding parentheses around the area code or adding dashes between the digits.
function wpsnippets_format_phone_number($value) {
// Remove any non-digit characters from the phone number
$phone_number = preg_replace('/[^0-9]/', '', $value);
// Format the phone number with parentheses and dashes
$formatted_phone_number = sprintf(
'(%s) %s-%s',
substr($phone_number, 0, 3),
substr($phone_number, 3, 3),
substr($phone_number, 6)
);
return $formatted_phone_number;
}
add_filter('acf/format_value/type=phone', 'wpsnippets_format_phone_number');
This code snippet defines a custom PHP function wpsnippets_format_phone_number
that takes the phone number value as input and formats it using parentheses and dashes. It uses regular expressions to remove any non-digit characters from the phone number. Then, it uses sprintf
to format the phone number with the desired pattern. Finally, the function returns the formatted phone number.
The add_filter
function is used to hook the wpsnippets_format_phone_number
function to the acf/format_value/type=phone
filter. This ensures that the function is applied to the phone number field value whenever it is retrieved from the database.
By using this code snippet, you can easily format phone numbers entered by users in a consistent and visually appealing way.
Examples
Example 1: Format phone number with parentheses and dashes
This use case demonstrates how to format a phone number field in ACF to display it with parentheses around the area code and dashes between the digits.
function wpsnippets_format_phone_number($value) {
$formatted_number = preg_replace('/^(d{3})(d{3})(d{4})$/', '($1) $2-$3', $value);
return $formatted_number;
}
add_filter('acf/format_value/type=phone', 'wpsnippets_format_phone_number');
In this code example, we define a custom PHP function wpsnippets_format_phone_number
that takes the phone number value as input and uses regular expressions to format it with parentheses and dashes. We then add a filter to the ACF acf/format_value/type=phone
hook, which applies our custom formatting function to the phone number field.
Example 2: Format phone number with country code
This use case demonstrates how to format a phone number field in ACF to include the country code.
function wpsnippets_format_phone_number($value) {
$formatted_number = '+1 ' . $value;
return $formatted_number;
}
add_filter('acf/format_value/type=phone', 'wpsnippets_format_phone_number');
In this code example, we define a custom PHP function wpsnippets_format_phone_number
that takes the phone number value as input and appends the country code “+1” to it. We then add a filter to the ACF acf/format_value/type=phone
hook, which applies our custom formatting function to the phone number field.
Example 3: Format phone number for international display
This use case demonstrates how to format a phone number field in ACF for international display, with the country code in parentheses and a space between the area code and the rest of the number.
function wpsnippets_format_phone_number($value) {
$formatted_number = preg_replace('/^(+d{1,3})(d{3})(d{3})(d{4})$/', '$1 ($2) $3 $4', $value);
return $formatted_number;
}
add_filter('acf/format_value/type=phone', 'wpsnippets_format_phone_number');
In this code example, we define a custom PHP function wpsnippets_format_phone_number
that takes the phone number value as input and uses regular expressions to format it for international display. The country code is enclosed in parentheses, and there is a space between the area code and the rest of the number. We then add a filter to the ACF acf/format_value/type=phone
hook, which applies our custom formatting function to the phone number field.