Home › Forums › General Issues › How to add a country list to ACF
Sadly WordPress doesn’t have a country list that can be accessed by various plugins in different scenarios. It does have a list of countries and cities in the time zone settings and WooCommerce installs its own country list available in setting up shops etc. Unfortunately a country list is not easily available in Advanced Custom Fields free or paid.
I found a free plugin on GitHub but it seems to either have a bug or is not yet ready for the 6.x series and it has no support. I also found a tutorial for making a country list plugin on Smashing Magazine (for 4.x) that led me eventually to an ACF example field type plugin template that should help me achieve my goal. However, it is so skimpy on instructions as to be useless to me not being a coder.
I followed all the instructions in the Readme and so far nothing shows in my available field types.
Can someone please take a look at the above-linked template and help guide me to getting a country dropdown list field.
I have a list with the codes and country names and also have it in a key/value pair array.
My two blockages are:
When I paste my 200+ line country list in a Select field type is works perfectly bu as I have three instances to select a country on one form, this means copy and pasting the list for each rather than just using an ACF Country Select Type.
Thanks.
The best way to create a massive select field is to dynamically populate the select field. Doing this you can create the filter once and apply it to as many fields as necessary.
https://www.advancedcustomfields.com/resources/dynamically-populate-a-select-fields-choices/
Thanks @John Huebner. It seems I am a little dense at this time of year. The page you referred me to seems to contain the solution to what I want but without clear instructions. It has many hints. For example, I surmise that (1) I will have to create an options page, (2) Configure it and add a text area, (3) Locate that page and save my list of countries in the text area, (4) collect some information about the page or text area, (5) include that information in the script to be added to a plugin or theme’s functions file.
I created the options page but can locate it nowhere except as a “page” to edit in ACF.
I guess I will have to delete that page and start again to see if there was something I missed along the way.
You don’t need to necessarily have anything else, you could simply provide a list of countries.
add_filter('acf/load_field/name=YOUR_FIELD_NAME', 'my_country_select');
function my_country_select($field) {
$field['choices'] = array(
'US' => 'United States',
'FR' => 'France',
// etc
);
return $field;
}
Or the list could be dynamically generated from another source.
https://gist.github.com/djaiss/2938259
I just posted a long reply saying thanks and that I got a PHP error when I added the block of code with my country array to the class file of the ACF example field type plugin template I downloaded from GitHub. When I added the code to my functions.php, I got no errors but am not sure if there was any effect on my custom Country List.
I ended up with an array on my from end form in place of the Country List field and after posting the array here got a message that I was banned from posting on this site.
Here’s my third attempt at posting this reply and asking where to place the block of code.
What I suggested is a function for functons.php. This is not something that needs a custom field type to accomplish. Please refer to to filter documentation.
I just realized that this is a discussion forum and not for support. I’ll move my query to support.
Thanks.
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.