Skip to content

Instantly share code, notes, and snippets.

@joviczarko
Last active August 4, 2024 10:46
Show Gist options
  • Save joviczarko/41afac1a49b314f0a49b308663af2459 to your computer and use it in GitHub Desktop.
Save joviczarko/41afac1a49b314f0a49b308663af2459 to your computer and use it in GitHub Desktop.
Import taxonomy countries but have country codes as slug
<?php function insert_country_terms() {
$taxonomy = 'country'; // Your custom taxonomy slug
// Define the countries hierarchy with country codes as values
$countries_hierarchy = array(
'Africa' => array(
'Algeria' => 'DZ',
'Angola' => 'AO',
'Benin' => 'BJ',
'Botswana' => 'BW',
'Burkina Faso' => 'BF',
'Burundi' => 'BI',
'Cabo Verde' => 'CV',
'Cameroon' => 'CM',
'Central African Republic' => 'CF',
'Chad' => 'TD',
'Comoros' => 'KM',
'Congo' => 'CG',
'Djibouti' => 'DJ',
'Egypt' => 'EG',
'Equatorial Guinea' => 'GQ',
'Eritrea' => 'ER',
'Eswatini' => 'SZ',
'Ethiopia' => 'ET',
'Gabon' => 'GA',
'Gambia' => 'GM',
'Ghana' => 'GH',
'Guinea' => 'GN',
'Guinea-Bissau' => 'GW',
'Ivory Coast' => 'CI',
'Kenya' => 'KE',
'Lesotho' => 'LS',
'Liberia' => 'LR',
'Libya' => 'LY',
'Madagascar' => 'MG',
'Malawi' => 'MW',
'Mali' => 'ML',
'Mauritania' => 'MR',
'Mauritius' => 'MU',
'Morocco' => 'MA',
'Mozambique' => 'MZ',
'Namibia' => 'NA',
'Niger' => 'NE',
'Nigeria' => 'NG',
'Rwanda' => 'RW',
'Sao Tome and Principe' => 'ST',
'Senegal' => 'SN',
'Seychelles' => 'SC',
'Sierra Leone' => 'SL',
'Somalia' => 'SO',
'South Africa' => 'ZA',
'South Sudan' => 'SS',
'Sudan' => 'SD',
'Tanzania' => 'TZ',
'Togo' => 'TG',
'Tunisia' => 'TN',
'Uganda' => 'UG',
'Zambia' => 'ZM',
'Zimbabwe' => 'ZW'
),
'Asia' => array(
'Afghanistan' => 'AF',
'Armenia' => 'AM',
'Azerbaijan' => 'AZ',
'Bahrain' => 'BH',
'Bangladesh' => 'BD',
'Bhutan' => 'BT',
'Brunei' => 'BN',
'Cambodia' => 'KH',
'China' => 'CN',
'Cyprus' => 'CY',
'Georgia' => 'GE',
'India' => 'IN',
'Indonesia' => 'ID',
'Iran' => 'IR',
'Iraq' => 'IQ',
'Israel' => 'IL',
'Japan' => 'JP',
'Jordan' => 'JO',
'Kazakhstan' => 'KZ',
'Kuwait' => 'KW',
'Kyrgyzstan' => 'KG',
'Laos' => 'LA',
'Lebanon' => 'LB',
'Malaysia' => 'MY',
'Maldives' => 'MV',
'Mongolia' => 'MN',
'Myanmar' => 'MM',
'Nepal' => 'NP',
'North Korea' => 'KP',
'Oman' => 'OM',
'Pakistan' => 'PK',
'Palestine' => 'PS',
'Philippines' => 'PH',
'Qatar' => 'QA',
'Saudi Arabia' => 'SA',
'Singapore' => 'SG',
'South Korea' => 'KR',
'Sri Lanka' => 'LK',
'Syria' => 'SY',
'Taiwan' => 'TW',
'Tajikistan' => 'TJ',
'Thailand' => 'TH',
'Timor-Leste' => 'TL',
'Turkey' => 'TR',
'Turkmenistan' => 'TM',
'United Arab Emirates' => 'AE',
'Uzbekistan' => 'UZ',
'Vietnam' => 'VN',
'Yemen' => 'YE'
),
'Europe' => array(
'Albania' => 'AL',
'Andorra' => 'AD',
'Austria' => 'AT',
'Belarus' => 'BY',
'Belgium' => 'BE',
'Bosnia and Herzegovina' => 'BA',
'Bulgaria' => 'BG',
'Croatia' => 'HR',
'Cyprus' => 'CY',
'Czech Republic' => 'CZ',
'Denmark' => 'DK',
'Estonia' => 'EE',
'Finland' => 'FI',
'France' => 'FR',
'Germany' => 'DE',
'Greece' => 'GR',
'Hungary' => 'HU',
'Iceland' => 'IS',
'Ireland' => 'IE',
'Italy' => 'IT',
'Kosovo' => 'XK',
'Latvia' => 'LV',
'Liechtenstein' => 'LI',
'Lithuania' => 'LT',
'Luxembourg' => 'LU',
'Malta' => 'MT',
'Moldova' => 'MD',
'Monaco' => 'MC',
'Montenegro' => 'ME',
'Netherlands' => 'NL',
'North Macedonia' => 'MK',
'Norway' => 'NO',
'Poland' => 'PL',
'Portugal' => 'PT',
'Romania' => 'RO',
'Russia' => 'RU',
'San Marino' => 'SM',
'Serbia' => 'RS',
'Slovakia' => 'SK',
'Slovenia' => 'SI',
'Spain' => 'ES',
'Sweden' => 'SE',
'Switzerland' => 'CH',
'Turkey' => 'TR',
'Ukraine' => 'UA',
'United Kingdom' => 'GB',
'Vatican City' => 'VA'
),
'North America' => array(
'Antigua and Barbuda' => 'AG',
'Bahamas' => 'BS',
'Barbados' => 'BB',
'Belize' => 'BZ',
'Canada' => 'CA',
'Costa Rica' => 'CR',
'Cuba' => 'CU',
'Dominica' => 'DM',
'Dominican Republic' => 'DO',
'El Salvador' => 'SV',
'Grenada' => 'GD',
'Guatemala' => 'GT',
'Haiti' => 'HT',
'Honduras' => 'HN',
'Jamaica' => 'JM',
'Mexico' => 'MX',
'Nicaragua' => 'NI',
'Panama' => 'PA',
'Saint Kitts and Nevis' => 'KN',
'Saint Lucia' => 'LC',
'Saint Vincent and the Grenadines' => 'VC',
'Trinidad and Tobago' => 'TT',
'United States' => 'US'
),
'Oceania' => array(
'Australia' => 'AU',
'Fiji' => 'FJ',
'Kiribati' => 'KI',
'Marshall Islands' => 'MH',
'Micronesia' => 'FM',
'Nauru' => 'NR',
'New Zealand' => 'NZ',
'Palau' => 'PW',
'Papua New Guinea' => 'PG',
'Samoa' => 'WS',
'Solomon Islands' => 'SB',
'Tonga' => 'TO',
'Tuvalu' => 'TV',
'Vanuatu' => 'VU'
),
'South America' => array(
'Argentina' => 'AR',
'Bolivia' => 'BO',
'Brazil' => 'BR',
'Chile' => 'CL',
'Colombia' => 'CO',
'Ecuador' => 'EC',
'Guyana' => 'GY',
'Paraguay' => 'PY',
'Peru' => 'PE',
'Suriname' => 'SR',
'Uruguay' => 'UY',
'Venezuela' => 'VE'
)
);
foreach ($countries_hierarchy as $region => $countries) {
// Insert the region term
if (!term_exists($region, $taxonomy)) {
$region_term = wp_insert_term($region, $taxonomy);
} else {
$region_term = get_term_by('name', $region, $taxonomy);
}
if (is_wp_error($region_term)) {
echo 'Failed to insert region: ' . $region . "\n";
continue;
}
$region_term_id = is_wp_error($region_term) ? $region_term->get_error_data() : $region_term['term_id'];
// Insert the country terms
foreach ($countries as $country_name => $country_slug) {
// Convert slug to lowercase
$country_slug = strtolower($country_slug);
// Check if the term with the specific slug exists
if (!term_exists($country_slug, $taxonomy)) {
// Insert term with country name and slug
wp_insert_term(
$country_name, // Term name
$taxonomy, // Taxonomy
array(
'slug' => $country_slug, // Term slug
'parent' => $region_term_id // Parent term ID
)
);
}
}
}
echo 'Terms have been imported successfully.' . "\n";
}
insert_country_terms();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment