Display a Country selector as radio, checkbox or select field type.
Setting name | Description |
Allow Countries | Filter which countries can be chosen |
Appearance | Select the appearance of this field |
Display Flag | Display flags next to the country name |
Group by Continent | Group countries by their continent |
Display Format | Use specific display format using template tags {localized} {native} {code} |
Default Value | Enter each default value on a new line |
Return Value | Return the country array, country name or country code |
Allow Null | Allow empty value |
Layout | Choose the layout |
Toggle | Allow to toggle all values |
Allow Custom | Allow custom value |
Save Custom | Save custom value in the field settings |
Select multiple values | Allow multiple values selection |
Stylised UI | Enable Select2 UI style |
$countries = get_field('countries');
// United States
$countries = get_field('countries');
// us
$countries = get_field('countries');
/**
* array(
* 'code' => 'us',
* 'name' => 'United States',
* 'localized' => 'United States',
* 'native' => 'United States',
* 'dial' => array(
* '1',
* ),
* 'capital' => 'Washington, D.C.',
* 'people' => 'American',
* 'continent' => 'America',
* 'coords' => array(
* 'lat' => 38.0,
* 'lng' => -97.0,
* ),
* 'languages' => array(
* 'en_US',
* ),
* 'currencies' => array(
* 'USD',
* ),
* )
*/
$countries = get_field('countries', false, false);
// us
/**
* acfe/fields/countries/query
*
* @array $args Query arguments
* @array $field Field settings
* @bool/string $post_id Current Post ID
*/
filter('acfe/fields/countries/query', $args, $field, $post_id);
filter('acfe/fields/countries/query/name=my_countries', $args, $field, $post_id);
filter('acfe/fields/countries/query/key=field_5ff50f25a59f6', $args, $field, $post_id);
add_filter('acfe/fields/countries/query/name=my_countries', 'my_acf_countries_query', 10, 3);
function my_acf_countries_query($args, $field, $post_id){
// change orderby & order
$args['orderby'] = 'name';
$args['order'] = 'DESC';
// return
return $args;
}
/**
* acfe/fields/countries/result
*
* @string $text Country result
* @array $country Country object
* @array $field Field settings
* @bool/string $post_id Current Post ID
*/
filter('acfe/fields/countries/result', $text, $country, $field, $post_id);
filter('acfe/fields/countries/result/name=my_countries', $text, $country, $field, $post_id);
filter('acfe/fields/countries/result/key=field_5ff50f25a59f6', $text, $country, $field, $post_id);
add_filter('acfe/fields/countries/result/name=my_countries', 'my_acf_countries_result', 10, 4);
function my_acf_countries_result($text, $country, $field, $post_id){
// change country result
$text = "<strong>{$text}</strong>";
// return
return $text;
}
It is possible to perform custom PHP queries and retrieve the result using the acfe_get_countries()
helper. Here are the default query arguments:
acfe_get_countries(array(
'code__in' => false,
'name__in' => false,
'continent__in' => false,
'language__in' => false,
'currency__in' => false,
'orderby' => false,
'order' => 'ASC',
'offset' => 0,
'limit' => -1,
'field' => false,
'display' => false,
'prepend' => false,
'append' => false,
'groupby' => false,
));
$country = acfe_get_country('fr');
/**
* array(
* 'code' => 'fr',
* 'name' => 'France',
* 'localized' => 'France',
* 'native' => 'France',
* 'dial' => array(
* '33',
* ),
* 'capital' => 'Paris',
* 'people' => 'French',
* 'continent' => 'Europe',
* 'coords' => array(
* 'lat' => 46.0,
* 'lng' => 2.0,
* ),
* 'languages' => array(
* 'eu',
* 'fr_FR',
* 'oci',
* ),
* 'currencies' => array(
* 'EUR',
* ),
* )
*/
$countries = acfe_get_countries(array(
'code__in' => array('us')
));
/**
* array(
* 'us' => array(
* 'code' => 'us',
* 'name' => 'United States',
* 'localized' => 'United States',
* 'native' => 'United States',
* 'dial' => array(
* '1',
* ),
* 'capital' => 'Washington, D.C.',
* 'people' => 'American',
* 'continent' => 'America',
* 'coords' => array(
* 'lat' => 38.0,
* 'lng' => -97.0,
* ),
* 'languages' => array(
* 'en_US',
* ),
* 'currencies' => array(
* 'USD',
* ),
* ),
* )
*/
$countries = acfe_get_countries(array(
'currency__in' => 'USD',
'field' => 'name',
'limit' => 3
));
/**
* array(
* 'as' => 'American Samoa',
* 'vg' => 'British Virgin Islands',
* 'kh' => 'Cambodia',
* )
*/
$countries = acfe_get_countries(array(
'code__in' => array('us', 'fr', 'gb'),
'field' => 'code',
'display' => 'Code: {code} | Country: {name} | Capital: {capital}'
));
/**
* array(
* 'fr' => 'Code: fr | Country: France | Capital: Paris',
* 'gb' => 'Code: gb | Country: United Kingdom | Capital: London',
* 'us' => 'Code: us | Country: United States | Capital: Washington, D.C.',
* )
*/