Add all Country List as a Custom Taxonomy

I found this country array online and cannot vouch for it’s accuracy. You can add this to your functions.php file in your current theme. Reload WordPress and it should add them. Once they are added, delete this code. I did put a check in there to make sure they aren’t added twice.

<?php function esoft_add_countries() { $country_array = array( 'AF'=&gt;'AFGHANISTAN',
        'AL'=&gt;'ALBANIA',
        'DZ'=&gt;'ALGERIA',
        'AS'=&gt;'AMERICAN SAMOA',
        'AD'=&gt;'ANDORRA',
        'AO'=&gt;'ANGOLA',
        'AI'=&gt;'ANGUILLA',
        'AQ'=&gt;'ANTARCTICA',
        'AG'=&gt;'ANTIGUA AND BARBUDA',
        'AR'=&gt;'ARGENTINA',
        'AM'=&gt;'ARMENIA',
        'AW'=&gt;'ARUBA',
        'AU'=&gt;'AUSTRALIA',
        'AT'=&gt;'AUSTRIA',
        'AZ'=&gt;'AZERBAIJAN',
        'BS'=&gt;'BAHAMAS',
        'BH'=&gt;'BAHRAIN',
        'BD'=&gt;'BANGLADESH',
        'BB'=&gt;'BARBADOS',
        'BY'=&gt;'BELARUS',
        'BE'=&gt;'BELGIUM',
        'BZ'=&gt;'BELIZE',
        'BJ'=&gt;'BENIN',
        'BM'=&gt;'BERMUDA',
        'BT'=&gt;'BHUTAN',
        'BO'=&gt;'BOLIVIA',
        'BA'=&gt;'BOSNIA AND HERZEGOVINA',
        'BW'=&gt;'BOTSWANA',
        'BV'=&gt;'BOUVET ISLAND',
        'BR'=&gt;'BRAZIL',
        'IO'=&gt;'BRITISH INDIAN OCEAN TERRITORY',
        'BN'=&gt;'BRUNEI DARUSSALAM',
        'BG'=&gt;'BULGARIA',
        'BF'=&gt;'BURKINA FASO',
        'BI'=&gt;'BURUNDI',
        'KH'=&gt;'CAMBODIA',
        'CM'=&gt;'CAMEROON',
        'CA'=&gt;'CANADA',
        'CV'=&gt;'CAPE VERDE',
        'KY'=&gt;'CAYMAN ISLANDS',
        'CF'=&gt;'CENTRAL AFRICAN REPUBLIC',
        'TD'=&gt;'CHAD',
        'CL'=&gt;'CHILE',
        'CN'=&gt;'CHINA',
        'CX'=&gt;'CHRISTMAS ISLAND',
        'CC'=&gt;'COCOS (KEELING) ISLANDS',
        'CO'=&gt;'COLOMBIA',
        'KM'=&gt;'COMOROS',
        'CG'=&gt;'CONGO',
        'CD'=&gt;'CONGO, THE DEMOCRATIC REPUBLIC OF THE',
        'CK'=&gt;'COOK ISLANDS',
        'CR'=&gt;'COSTA RICA',
        'CI'=&gt;'COTE D IVOIRE',
        'HR'=&gt;'CROATIA',
        'CU'=&gt;'CUBA',
        'CY'=&gt;'CYPRUS',
        'CZ'=&gt;'CZECH REPUBLIC',
        'DK'=&gt;'DENMARK',
        'DJ'=&gt;'DJIBOUTI',
        'DM'=&gt;'DOMINICA',
        'DO'=&gt;'DOMINICAN REPUBLIC',
        'TP'=&gt;'EAST TIMOR',
        'EC'=&gt;'ECUADOR',
        'EG'=&gt;'EGYPT',
        'SV'=&gt;'EL SALVADOR',
        'GQ'=&gt;'EQUATORIAL GUINEA',
        'ER'=&gt;'ERITREA',
        'EE'=&gt;'ESTONIA',
        'ET'=&gt;'ETHIOPIA',
        'FK'=&gt;'FALKLAND ISLANDS (MALVINAS)',
        'FO'=&gt;'FAROE ISLANDS',
        'FJ'=&gt;'FIJI',
        'FI'=&gt;'FINLAND',
        'FR'=&gt;'FRANCE',
        'GF'=&gt;'FRENCH GUIANA',
        'PF'=&gt;'FRENCH POLYNESIA',
        'TF'=&gt;'FRENCH SOUTHERN TERRITORIES',
        'GA'=&gt;'GABON',
        'GM'=&gt;'GAMBIA',
        'GE'=&gt;'GEORGIA',
        'DE'=&gt;'GERMANY',
        'GH'=&gt;'GHANA',
        'GI'=&gt;'GIBRALTAR',
        'GR'=&gt;'GREECE',
        'GL'=&gt;'GREENLAND',
        'GD'=&gt;'GRENADA',
        'GP'=&gt;'GUADELOUPE',
        'GU'=&gt;'GUAM',
        'GT'=&gt;'GUATEMALA',
        'GN'=&gt;'GUINEA',
        'GW'=&gt;'GUINEA-BISSAU',
        'GY'=&gt;'GUYANA',
        'HT'=&gt;'HAITI',
        'HM'=&gt;'HEARD ISLAND AND MCDONALD ISLANDS',
        'VA'=&gt;'HOLY SEE (VATICAN CITY STATE)',
        'HN'=&gt;'HONDURAS',
        'HK'=&gt;'HONG KONG',
        'HU'=&gt;'HUNGARY',
        'IS'=&gt;'ICELAND',
        'IN'=&gt;'INDIA',
        'ID'=&gt;'INDONESIA',
        'IR'=&gt;'IRAN, ISLAMIC REPUBLIC OF',
        'IQ'=&gt;'IRAQ',
        'IE'=&gt;'IRELAND',
        'IL'=&gt;'ISRAEL',
        'IT'=&gt;'ITALY',
        'JM'=&gt;'JAMAICA',
        'JP'=&gt;'JAPAN',
        'JO'=&gt;'JORDAN',
        'KZ'=&gt;'KAZAKSTAN',
        'KE'=&gt;'KENYA',
        'KI'=&gt;'KIRIBATI',
        'KP'=&gt;'KOREA DEMOCRATIC PEOPLES REPUBLIC OF',
        'KR'=&gt;'KOREA REPUBLIC OF',
        'KW'=&gt;'KUWAIT',
        'KG'=&gt;'KYRGYZSTAN',
        'LA'=&gt;'LAO PEOPLES DEMOCRATIC REPUBLIC',
        'LV'=&gt;'LATVIA',
        'LB'=&gt;'LEBANON',
        'LS'=&gt;'LESOTHO',
        'LR'=&gt;'LIBERIA',
        'LY'=&gt;'LIBYAN ARAB JAMAHIRIYA',
        'LI'=&gt;'LIECHTENSTEIN',
        'LT'=&gt;'LITHUANIA',
        'LU'=&gt;'LUXEMBOURG',
        'MO'=&gt;'MACAU',
        'MK'=&gt;'MACEDONIA, THE FORMER YUGOSLAV REPUBLIC OF',
        'MG'=&gt;'MADAGASCAR',
        'MW'=&gt;'MALAWI',
        'MY'=&gt;'MALAYSIA',
        'MV'=&gt;'MALDIVES',
        'ML'=&gt;'MALI',
        'MT'=&gt;'MALTA',
        'MH'=&gt;'MARSHALL ISLANDS',
        'MQ'=&gt;'MARTINIQUE',
        'MR'=&gt;'MAURITANIA',
        'MU'=&gt;'MAURITIUS',
        'YT'=&gt;'MAYOTTE',
        'MX'=&gt;'MEXICO',
        'FM'=&gt;'MICRONESIA, FEDERATED STATES OF',
        'MD'=&gt;'MOLDOVA, REPUBLIC OF',
        'MC'=&gt;'MONACO',
        'MN'=&gt;'MONGOLIA',
        'MS'=&gt;'MONTSERRAT',
        'MA'=&gt;'MOROCCO',
        'MZ'=&gt;'MOZAMBIQUE',
        'MM'=&gt;'MYANMAR',
        'NA'=&gt;'NAMIBIA',
        'NR'=&gt;'NAURU',
        'NP'=&gt;'NEPAL',
        'NL'=&gt;'NETHERLANDS',
        'AN'=&gt;'NETHERLANDS ANTILLES',
        'NC'=&gt;'NEW CALEDONIA',
        'NZ'=&gt;'NEW ZEALAND',
        'NI'=&gt;'NICARAGUA',
        'NE'=&gt;'NIGER',
        'NG'=&gt;'NIGERIA',
        'NU'=&gt;'NIUE',
        'NF'=&gt;'NORFOLK ISLAND',
        'MP'=&gt;'NORTHERN MARIANA ISLANDS',
        'NO'=&gt;'NORWAY',
        'OM'=&gt;'OMAN',
        'PK'=&gt;'PAKISTAN',
        'PW'=&gt;'PALAU',
        'PS'=&gt;'PALESTINIAN TERRITORY, OCCUPIED',
        'PA'=&gt;'PANAMA',
        'PG'=&gt;'PAPUA NEW GUINEA',
        'PY'=&gt;'PARAGUAY',
        'PE'=&gt;'PERU',
        'PH'=&gt;'PHILIPPINES',
        'PN'=&gt;'PITCAIRN',
        'PL'=&gt;'POLAND',
        'PT'=&gt;'PORTUGAL',
        'PR'=&gt;'PUERTO RICO',
        'QA'=&gt;'QATAR',
        'RE'=&gt;'REUNION',
        'RO'=&gt;'ROMANIA',
        'RU'=&gt;'RUSSIAN FEDERATION',
        'RW'=&gt;'RWANDA',
        'SH'=&gt;'SAINT HELENA',
        'KN'=&gt;'SAINT KITTS AND NEVIS',
        'LC'=&gt;'SAINT LUCIA',
        'PM'=&gt;'SAINT PIERRE AND MIQUELON',
        'VC'=&gt;'SAINT VINCENT AND THE GRENADINES',
        'WS'=&gt;'SAMOA',
        'SM'=&gt;'SAN MARINO',
        'ST'=&gt;'SAO TOME AND PRINCIPE',
        'SA'=&gt;'SAUDI ARABIA',
        'SN'=&gt;'SENEGAL',
        'SC'=&gt;'SEYCHELLES',
        'SL'=&gt;'SIERRA LEONE',
        'SG'=&gt;'SINGAPORE',
        'SK'=&gt;'SLOVAKIA',
        'SI'=&gt;'SLOVENIA',
        'SB'=&gt;'SOLOMON ISLANDS',
        'SO'=&gt;'SOMALIA',
        'ZA'=&gt;'SOUTH AFRICA',
        'GS'=&gt;'SOUTH GEORGIA AND THE SOUTH SANDWICH ISLANDS',
        'ES'=&gt;'SPAIN',
        'LK'=&gt;'SRI LANKA',
        'SD'=&gt;'SUDAN',
        'SR'=&gt;'SURINAME',
        'SJ'=&gt;'SVALBARD AND JAN MAYEN',
        'SZ'=&gt;'SWAZILAND',
        'SE'=&gt;'SWEDEN',
        'CH'=&gt;'SWITZERLAND',
        'SY'=&gt;'SYRIAN ARAB REPUBLIC',
        'TW'=&gt;'TAIWAN, PROVINCE OF CHINA',
        'TJ'=&gt;'TAJIKISTAN',
        'TZ'=&gt;'TANZANIA, UNITED REPUBLIC OF',
        'TH'=&gt;'THAILAND',
        'TG'=&gt;'TOGO',
        'TK'=&gt;'TOKELAU',
        'TO'=&gt;'TONGA',
        'TT'=&gt;'TRINIDAD AND TOBAGO',
        'TN'=&gt;'TUNISIA',
        'TR'=&gt;'TURKEY',
        'TM'=&gt;'TURKMENISTAN',
        'TC'=&gt;'TURKS AND CAICOS ISLANDS',
        'TV'=&gt;'TUVALU',
        'UG'=&gt;'UGANDA',
        'UA'=&gt;'UKRAINE',
        'AE'=&gt;'UNITED ARAB EMIRATES',
        'GB'=&gt;'UNITED KINGDOM',
        'US'=&gt;'UNITED STATES',
        'UM'=&gt;'UNITED STATES MINOR OUTLYING ISLANDS',
        'UY'=&gt;'URUGUAY',
        'UZ'=&gt;'UZBEKISTAN',
        'VU'=&gt;'VANUATU',
        'VE'=&gt;'VENEZUELA',
        'VN'=&gt;'VIET NAM',
        'VG'=&gt;'VIRGIN ISLANDS, BRITISH',
        'VI'=&gt;'VIRGIN ISLANDS, U.S.',
        'WF'=&gt;'WALLIS AND FUTUNA',
        'EH'=&gt;'WESTERN SAHARA',
        'YE'=&gt;'YEMEN',
        'YU'=&gt;'YUGOSLAVIA',
        'ZM'=&gt;'ZAMBIA',
        'ZW'=&gt;'ZIMBABWE',
    );

    // Loop through array and insert terms
    foreach($country_array as $abbr =&gt; $name)
    {
        if(!get_term_by('name', ucwords(strtolower($name)), 'country'))
            wp_insert_term(ucwords(strtolower($name)), 'country');
    }
}
add_action('init', 'esoft_add_countries', 100);
?>

I’ve tried some tutorials and some guides found by Google but none which has yet to work. Add bellows php code on your function.php file for register custom taxonomy like ‘country’.

 

// Register Custom Taxonomy
function country_taxonomy() {

  $labels = array(
    'name'                       => _x( 'Taxonomies', 'Taxonomy General Name', 'text_domain' ),
    'singular_name'              => _x( 'Taxonomy', 'Taxonomy Singular Name', 'text_domain' ),
    'menu_name'                  => __( 'Taxonomy', 'text_domain' ),
    'all_items'                  => __( 'All Items', 'text_domain' ),
    'parent_item'                => __( 'Parent Item', 'text_domain' ),
    'parent_item_colon'          => __( 'Parent Item:', 'text_domain' ),
    'new_item_name'              => __( 'New Item Name', 'text_domain' ),
    'add_new_item'               => __( 'Add New Item', 'text_domain' ),
    'edit_item'                  => __( 'Edit Item', 'text_domain' ),
    'update_item'                => __( 'Update Item', 'text_domain' ),
    'view_item'                  => __( 'View Item', 'text_domain' ),
    'separate_items_with_commas' => __( 'Separate items with commas', 'text_domain' ),
    'add_or_remove_items'        => __( 'Add or remove items', 'text_domain' ),
    'choose_from_most_used'      => __( 'Choose from the most used', 'text_domain' ),
    'popular_items'              => __( 'Popular Items', 'text_domain' ),
    'search_items'               => __( 'Search Items', 'text_domain' ),
    'not_found'                  => __( 'Not Found', 'text_domain' ),
    'no_terms'                   => __( 'No items', 'text_domain' ),
    'items_list'                 => __( 'Items list', 'text_domain' ),
    'items_list_navigation'      => __( 'Items list navigation', 'text_domain' ),
  );
  $args = array(
    'labels'                     => $labels,
    'hierarchical'               => false,
    'public'                     => true,
    'show_ui'                    => true,
    'show_admin_column'          => true,
    'show_in_nav_menus'          => true,
    'show_tagcloud'              => true,
  );
  register_taxonomy( 'taxonomy', array( 'post' ), $args );

}
add_action( 'init', 'country_taxonomy', 0 );

Also need to display drop-down menu on sidebar of WordPress admin dashboard. So you have to add bellows code for this on your function.php file. This suppose you have normal post type “post” and a custom taxonomy “country”.

 

<?php 
function add_country_meta_box() 
{ 
remove_meta_box( 'country', 'post', 'side' ); 
add_meta_box( 'country', 'country', 'country_meta_box', 'post', 'side' );
} 
add_action('add_meta_boxes', 'add_country_meta_box'); 
/* Prints the taxonomy box content */ 
function country_meta_box($post) 
{ 
$taxonomy = get_taxonomy('country'); 
}
?>

 

 

Leave a Comment

Your email address will not be published.