Is there a shorter way to get all countries populated options, instead of writing options one by one, in the following dropdown?
<select name="input-country" id="input-country"
class="form-control form-control-alternative">
<option value="">Select a country / region…</option>
<option value="AF">Afghanistan</option>
<option value="AX">Åland Islands</option>
<option value="AL">Albania</option>
</select>
Or how do get a dropdown of all countries in WordPress?
2
Answers
The following custom shortcode will display a country dropdown:
Code goes in functions.php file of your child theme (or in a plugin). Tested and works.
Usage:
[countries]
;echo do_shortcode('[countries]');
Here’s an example of how you might do it in PHP using a custom array of countries:
In this example, you create an associative array $countries where each key is a country’s ISO Alpha-2 code and each value is the country’s name. The generate_country_options function loops through this array and builds the option tags for the dropdown.