skip to Main Content

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


  1. The following custom shortcode will display a country dropdown:

    // Utility function: Return an array of countries (code / name pairs)
    function get_all_countries() {
        return array(
            'AF' => __('Afghanistan'),
            'AX' => __('Åland Islands'),
            'AL' => __('Albania'),
            'DZ' => __('Algeria'),
            'AS' => __('American Samoa'),
            'AD' => __('Andorra'),
            'AO' => __('Angola'),
            'AI' => __('Anguilla'),
            'AQ' => __('Antarctica'),
            'AG' => __('Antigua and Barbuda'),
            'AR' => __('Argentina'),
            'AM' => __('Armenia'),
            'AW' => __('Aruba'),
            'AU' => __('Australia'),
            'AT' => __('Austria'),
            'AZ' => __('Azerbaijan'),
            'BS' => __('Bahamas'),
            'BH' => __('Bahrain'),
            'BD' => __('Bangladesh'),
            'BB' => __('Barbados'),
            'BY' => __('Belarus'),
            'PW' => __('Belau'),
            'BE' => __('Belgium'),
            'BZ' => __('Belize'),
            'BJ' => __('Benin'),
            'BM' => __('Bermuda'),
            'BT' => __('Bhutan'),
            'BO' => __('Bolivia'),
            'BQ' => __('Bonaire, Saint Eustatius and Saba'),
            'BA' => __('Bosnia and Herzegovina'),
            'BW' => __('Botswana'),
            'BV' => __('Bouvet Island'),
            'BR' => __('Brazil'),
            'IO' => __('British Indian Ocean Territory'),
            'BN' => __('Brunei'),
            'BG' => __('Bulgaria'),
            'BF' => __('Burkina Faso'),
            'BI' => __('Burundi'),
            'KH' => __('Cambodia'),
            'CM' => __('Cameroon'),
            'CA' => __('Canada'),
            'CV' => __('Cape Verde'),
            'KY' => __('Cayman Islands'),
            'CF' => __('Central African Republic'),
            'TD' => __('Chad'),
            'CL' => __('Chile'),
            'CN' => __('China'),
            'CX' => __('Christmas Island'),
            'CC' => __('Cocos (Keeling) Islands'),
            'CO' => __('Colombia'),
            'KM' => __('Comoros'),
            'CG' => __('Congo (Brazzaville)'),
            'CD' => __('Congo (Kinshasa)'),
            'CK' => __('Cook Islands'),
            'CR' => __('Costa Rica'),
            'HR' => __('Croatia'),
            'CU' => __('Cuba'),
            'CW' => __('Curaçao'),
            'CY' => __('Cyprus'),
            'CZ' => __('Czech Republic'),
            'DK' => __('Denmark'),
            'DJ' => __('Djibouti'),
            'DM' => __('Dominica'),
            'DO' => __('Dominican Republic'),
            'EC' => __('Ecuador'),
            'EG' => __('Egypt'),
            'SV' => __('El Salvador'),
            'GQ' => __('Equatorial Guinea'),
            'ER' => __('Eritrea'),
            'EE' => __('Estonia'),
            'SZ' => __('Eswatini'),
            'ET' => __('Ethiopia'),
            'FK' => __('Falkland Islands'),
            'FO' => __('Faroe Islands'),
            'FJ' => __('Fiji'),
            'FI' => __('Finland'),
            'FR' => __('France'),
            'GF' => __('French Guiana'),
            'PF' => __('French Polynesia'),
            'TF' => __('French Southern Territories'),
            'GA' => __('Gabon'),
            'GM' => __('Gambia'),
            'GE' => __('Georgia'),
            'DE' => __('Germany'),
            'GH' => __('Ghana'),
            'GI' => __('Gibraltar'),
            'GR' => __('Greece'),
            'GL' => __('Greenland'),
            'GD' => __('Grenada'),
            'GP' => __('Guadeloupe'),
            'GU' => __('Guam'),
            'GT' => __('Guatemala'),
            'GG' => __('Guernsey'),
            'GN' => __('Guinea'),
            'GW' => __('Guinea-Bissau'),
            'GY' => __('Guyana'),
            'HT' => __('Haiti'),
            'HM' => __('Heard Island and McDonald Islands'),
            'HN' => __('Honduras'),
            'HK' => __('Hong Kong'),
            'HU' => __('Hungary'),
            'IS' => __('Iceland'),
            'IN' => __('India'),
            'ID' => __('Indonesia'),
            'IR' => __('Iran'),
            'IQ' => __('Iraq'),
            'IE' => __('Ireland'),
            'IM' => __('Isle of Man'),
            'IL' => __('Israel'),
            'IT' => __('Italy'),
            'CI' => __('Ivory Coast'),
            'JM' => __('Jamaica'),
            'JP' => __('Japan'),
            'JE' => __('Jersey'),
            'JO' => __('Jordan'),
            'KZ' => __('Kazakhstan'),
            'KE' => __('Kenya'),
            'KI' => __('Kiribati'),
            'KW' => __('Kuwait'),
            'KG' => __('Kyrgyzstan'),
            'LA' => __('Laos'),
            'LV' => __('Latvia'),
            'LB' => __('Lebanon'),
            'LS' => __('Lesotho'),
            'LR' => __('Liberia'),
            'LY' => __('Libya'),
            'LI' => __('Liechtenstein'),
            'LT' => __('Lithuania'),
            'LU' => __('Luxembourg'),
            'MO' => __('Macao'),
            'MG' => __('Madagascar'),
            'MW' => __('Malawi'),
            'MY' => __('Malaysia'),
            'MV' => __('Maldives'),
            'ML' => __('Mali'),
            'MT' => __('Malta'),
            'MH' => __('Marshall Islands'),
            'MQ' => __('Martinique'),
            'MR' => __('Mauritania'),
            'MU' => __('Mauritius'),
            'YT' => __('Mayotte'),
            'MX' => __('Mexico'),
            'FM' => __('Micronesia'),
            'MD' => __('Moldova'),
            'MC' => __('Monaco'),
            'MN' => __('Mongolia'),
            'ME' => __('Montenegro'),
            'MS' => __('Montserrat'),
            'MA' => __('Morocco'),
            'MZ' => __('Mozambique'),
            'MM' => __('Myanmar'),
            'NA' => __('Namibia'),
            'NR' => __('Nauru'),
            'NP' => __('Nepal'),
            'NL' => __('Netherlands'),
            'NC' => __('New Caledonia'),
            'NZ' => __('New Zealand'),
            'NI' => __('Nicaragua'),
            'NE' => __('Niger'),
            'NG' => __('Nigeria'),
            'NU' => __('Niue'),
            'NF' => __('Norfolk Island'),
            'KP' => __('North Korea'),
            'MK' => __('North Macedonia'),
            'MP' => __('Northern Mariana Islands'),
            'NO' => __('Norway'),
            'OM' => __('Oman'),
            'PK' => __('Pakistan'),
            'PS' => __('Palestinian Territory'),
            'PA' => __('Panama'),
            'PG' => __('Papua New Guinea'),
            'PY' => __('Paraguay'),
            'PE' => __('Peru'),
            'PH' => __('Philippines'),
            'PN' => __('Pitcairn'),
            'PL' => __('Poland'),
            'PT' => __('Portugal'),
            'PR' => __('Puerto Rico'),
            'QA' => __('Qatar'),
            'RE' => __('Reunion'),
            'RO' => __('Romania'),
            'RU' => __('Russia'),
            'RW' => __('Rwanda'),
            'ST' => __('São Tomé and Príncipe'),
            'BL' => __('Saint Barthélemy'),
            'SH' => __('Saint Helena'),
            'KN' => __('Saint Kitts and Nevis'),
            'LC' => __('Saint Lucia'),
            'SX' => __('Saint Martin (Dutch part)'),
            'MF' => __('Saint Martin (French part)'),
            'PM' => __('Saint Pierre and Miquelon'),
            'VC' => __('Saint Vincent and the Grenadines'),
            'WS' => __('Samoa'),
            'SM' => __('San Marino'),
            'SA' => __('Saudi Arabia'),
            'SN' => __('Senegal'),
            'RS' => __('Serbia'),
            'SC' => __('Seychelles'),
            'SL' => __('Sierra Leone'),
            'SG' => __('Singapore'),
            'SK' => __('Slovakia'),
            'SI' => __('Slovenia'),
            'SB' => __('Solomon Islands'),
            'SO' => __('Somalia'),
            'ZA' => __('South Africa'),
            'GS' => __('South Georgia/Sandwich Islands'),
            'KR' => __('South Korea'),
            'SS' => __('South Sudan'),
            'ES' => __('Spain'),
            'LK' => __('Sri Lanka'),
            'SD' => __('Sudan'),
            'SR' => __('Suriname'),
            'SJ' => __('Svalbard and Jan Mayen'),
            'SE' => __('Sweden'),
            'CH' => __('Switzerland'),
            'SY' => __('Syria'),
            'TW' => __('Taiwan'),
            'TJ' => __('Tajikistan'),
            'TZ' => __('Tanzania'),
            'TH' => __('Thailand'),
            'TL' => __('Timor-Leste'),
            'TG' => __('Togo'),
            'TK' => __('Tokelau'),
            'TO' => __('Tonga'),
            'TT' => __('Trinidad and Tobago'),
            'TN' => __('Tunisia'),
            'TR' => __('Turkey'),
            'TM' => __('Turkmenistan'),
            'TC' => __('Turks and Caicos Islands'),
            'TV' => __('Tuvalu'),
            'UG' => __('Uganda'),
            'UA' => __('Ukraine'),
            'AE' => __('United Arab Emirates'),
            'GB' => __('United Kingdom (UK)'),
            'US' => __('United States (US)'),
            'UM' => __('United States (US) Minor Outlying Islands'),
            'UY' => __('Uruguay'),
            'UZ' => __('Uzbekistan'),
            'VU' => __('Vanuatu'),
            'VA' => __('Vatican'),
            'VE' => __('Venezuela'),
            'VN' => __('Vietnam'),
            'VG' => __('Virgin Islands (British)'),
            'VI' => __('Virgin Islands (US)'),
            'WF' => __('Wallis and Futuna'),
            'EH' => __('Western Sahara'),
            'YE' => __('Yemen'),
            'ZM' => __('Zambia'),
            'ZW' => __('Zimbabwe'),
        );
    }
    
    // Shortcode that displays a country dropdown
    add_shortcode  ('countries', 'countries_dropdown' ); 
    function countries_dropdown() {
       $html = '<select name="input-country" id="input-country" class="form-control form-control-alternative">
       <option value="">'.__('Select a country / region…').'</option>';
    
        foreach ( get_all_countries() as $code => $name ) {
            $html .= sprintf('<option value="%s">%s</option>', $code, $name );
        }
        return $html . '</select>';
    }
    

    Code goes in functions.php file of your child theme (or in a plugin). Tested and works.

    Usage:

    • In WordPress Post/page Editor, and some widgets: [countries];
    • In PHP files: echo do_shortcode('[countries]');
    Login or Signup to reply.
  2. Here’s an example of how you might do it in PHP using a custom array of countries:

    <?php
    // Array of countries with their ISO Alpha-2 codes
    $countries = array(
        "AF" => "Afghanistan",
        "AX" => "Åland Islands",
        "AL" => "Albania",
        // ... Add all other countries
    );
    
    // Function to generate the options for the select dropdown
    function generate_country_options($countries) {
        $options = '';
        foreach ($countries as $code => $name) {
            $options .= "<option value="$code">$name</option>n";
        }
        return $options;
    }
    ?>
    
    <select name="input-country" id="input-country" class="form-control form- 
    control-alternative">
        <option value="">Select a country / region…</option>
        <?php echo generate_country_options($countries); ?>
    </select>
    

    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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search