Phone Number PRO

Docs Fields Phone Number

#Field Render

This field uses the third-party Intl Tel Input library.

Display a Phone Number field with multiple customization settings, such as: Country selector, phone prefix, geolocation and more.

Field Group

#Field Settings

Setting nameDescription
Allow CountriesFilter which countries can be chosen
Preferred CountriesDefine the countries to appear at the top of the list
Default CountrySet the initial country selection
GeolocationLookup the user’s country based on their IP address
Native NamesShow native country names
National ModeAllow users to enter national numbers
Allow DropdownWhether or not to allow the dropdown
Separate Dial CodeDisplay the country dial code next to the selected flag
Default ValueMust be international number with prefix. ie: +1201-555-0123
PlaceholderYou may use {placeholder} to print the country phone number placeholder
Return ValueReturn the phone number array or the phone number

#Phone Number Addon

If you need to access advanced phone number formatting (national, international…), we recommend to install the libphonenumber for PHP library. ACF Extended does not include that library by default as it weighs more than 10MB.

#Field Value

Format: Phone Number

$phone_number = get_field('phone_number');

// +12015550123

Format: Phone Array

$phone_number = get_field('phone_number');

/**
 * array(
 *     'number'        => '+12015550123',
 *     'country'       => 'us',
 *     'national'      => '+12015550123',
 *     'international' => '+12015550123',
 * )
 */

Format: National Number

$phone_number = get_field('phone_number');

// +12015550123

Format: International Number

$phone_number = get_field('phone_number');

// +12015550123

Unformatted Value

$phone_number = get_field('phone_number', false, false);

// +12015550123

Format: Phone Number

$phone_number = get_field('phone_number');

// +12015550123

Format: Phone Array

$phone_number = get_field('phone_number');

/**
 * array(
 *     'number'        => '+12015550123',
 *     'country'       => 'us',
 *     'national'      => '(201) 555-0123',
 *     'international' => '+1 201-555-0123',
 * )
 */

Format: National Number

$phone_number = get_field('phone_number');

// (201) 555-0123

Format: International Number

$phone_number = get_field('phone_number');

// +1 201-555-0123

Unformatted Value

$phone_number = get_field('phone_number', false, false);

// +12015550123

#Geolocation

This field uses the third-party IPInfo.io geolocation service by default.

You can customize the API URL using the following filter. The request is done via GET. If you need further customization, please check the Custom Query hook.

/**
 * acfe/fields/phone_number/geolocation/api_params
 * 
 * @array   $params   API parameters
 * @array   $field    Field settings
 */
filter('acfe/fields/phone_number/geolocation/api_params',                         $params, $field);
filter('acfe/fields/phone_number/geolocation/api_params/name=my_phone_number',    $params, $field);
filter('acfe/fields/phone_number/geolocation/api_params/key=field_5ff50f25a59f6', $params, $field);
add_filter('acfe/fields/phone_number/geolocation/api_params/name=my_phone', 'my_acf_phone_api_params', 10, 2);
function my_acf_phone_api_params($params, $field){

    // get client ip
    $ip = acfe_get_ip();
    if(empty($ip)){
        return $params;
    }
    
    // customize api endpoint
    $params['url'] = "https://ipinfo.io/{$ip}/json";

    // customize the key to retrieve the country
    // this is the array key to search for in the api response
    // in this example, it will look for $response['country']
    $params['country_key'] = 'country';
    
    // return
    return $params;

}

 

You can perform your own custom API request, instead of the default one, using the following hook. In this filter, you have to return a ISO 3166-1 alpha-2 country code string. Example: US, FR or IE.

If you return null or $null (the first argument), the hook will fallback to the default geolocation api request. If you return false it will stop all api requests (including the default).

/**
 * acfe/fields/phone_number/geolocation/custom_query
 * 
 * @array   $null    Fallback
 * @array   $field   Field settings
 */
filter('acfe/fields/phone_number/geolocation/custom_query',                         $null, $field);
filter('acfe/fields/phone_number/geolocation/custom_query/name=my_phone_number',    $null, $field);
filter('acfe/fields/phone_number/geolocation/custom_query/key=field_5ff50f25a59f6', $null, $field);
add_filter('acfe/fields/phone_number/geolocation/custom_query/name=my_phone', 'my_acf_phone_custom_query', 10, 2);
function my_acf_phone_custom_query($null, $field){

    // get client ip
    $ip = acfe_get_ip();
    if(empty($ip)){
        return $null; // fallback to default geolocation
    }
    
    // custom api request
    $request = wp_remote_get("https://your-geolocation.com/ip/{$ip}");
    if(is_wp_error($request)){
        return $null; // fallback to default geolocation
    }

    // decode response
    $response = wp_remote_retrieve_body($request);
    $response = json_decode($response, true);

    // validate response
    if(!is_array($response) || !isset($response['country_code'])){
        return $null; // fallback to default geolocation
    }

    // return the country
    return $response['country_code'];

    // or stop all api request
    // return false;

}