Tulipe Media

Réflexions entrepreneuriales

BuddyPress – Create an automatic country dropdown list in profile field

Are you looking for a function that imports all countries of the world to a BuddyPress profile field called “Country” ? This code is for you.

Consider that you open an international BuddyPress social network and you want let your users select their country. It’s annoying to manually add countries one by one. The solution is to import all the countries with this piece of code, that you can find on GitHub.

Be careful, you have to copy and paste this code in the bp-custom.php file of your BuddyPress install. If you don’t have this file, create it and upload it to the /wp-content/ folder, and add this code inside it:

[code]

function bp_add_custom_country_list() {

if ( !xprofile_get_field_id_from_name(‘Country’) && ‘bp-profile-setup’ == $_GET[‘page’] ) {

$country_list_args = array(
‘field_group_id’ => 1,
‘name’ => ‘Country’,
‘description’ => ‘Please select your country’,
‘can_delete’ => true,
‘field_order’ => 2,
‘is_required’ => false,
‘type’ => ‘selectbox’,
‘order_by’ => ‘custom’

);

$country_list_id = xprofile_insert_field( $country_list_args );

if ( $country_list_id ) {

$countries = array(
“Afghanistan”,
“Albania”,
“Algeria”,
“Andorra”,
“Angola”,
“Antigua and Barbuda”,
“Argentina”,
“Armenia”,
“Australia”,
“Austria”,
“Azerbaijan”,
“Bahamas”,
“Bahrain”,
“Bangladesh”,
“Barbados”,
“Belarus”,
“Belgium”,
“Belize”,
“Benin”,
“Bhutan”,
“Bolivia”,
“Bosnia and Herzegovina”,
“Botswana”,
“Brazil”,
“Brunei”,
“Bulgaria”,
“Burkina Faso”,
“Burundi”,
“Cambodia”,
“Cameroon”,
“Canada”,
“Cape Verde”,
“Central African Republic”,
“Chad”,
“Chile”,
“China”,
“Colombi”,
“Comoros”,
“Congo (Brazzaville)”,
“Congo”,
“Costa Rica”,
“Cote d’Ivoire”,
“Croatia”,
“Cuba”,
“Cyprus”,
“Czech Republic”,
“Denmark”,
“Djibouti”,
“Dominica”,
“Dominican Republic”,
“East Timor (Timor Timur)”,
“Ecuador”,
“Egypt”,
“El Salvador”,
“Equatorial Guinea”,
“Eritrea”,
“Estonia”,
“Ethiopia”,
“Fiji”,
“Finland”,
“France”,
“Gabon”,
“Gambia, The”,
“Georgia”,
“Germany”,
“Ghana”,
“Greece”,
“Grenada”,
“Guatemala”,
“Guinea”,
“Guinea-Bissau”,
“Guyana”,
“Haiti”,
“Honduras”,
“Hungary”,
“Iceland”,
“India”,
“Indonesia”,
“Iran”,
“Iraq”,
“Ireland”,
“Israel”,
“Italy”,
“Jamaica”,
“Japan”,
“Jordan”,
“Kazakhstan”,
“Kenya”,
“Kiribati”,
“Korea, North”,
“Korea, South”,
“Kuwait”,
“Kyrgyzstan”,
“Laos”,
“Latvia”,
“Lebanon”,
“Lesotho”,
“Liberia”,
“Libya”,
“Liechtenstein”,
“Lithuania”,
“Luxembourg”,
“Macedonia”,
“Madagascar”,
“Malawi”,
“Malaysia”,
“Maldives”,
“Mali”,
“Malta”,
“Marshall Islands”,
“Mauritania”,
“Mauritius”,
“Mexico”,
“Micronesia”,
“Moldova”,
“Monaco”,
“Mongolia”,
“Morocco”,
“Mozambique”,
“Myanmar”,
“Namibia”,
“Nauru”,
“Nepal”,
“Netherlands”,
“New Zealand”,
“Nicaragua”,
“Niger”,
“Nigeria”,
“Norway”,
“Oman”,
“Pakistan”,
“Palau”,
“Panama”,
“Papua New Guinea”,
“Paraguay”,
“Peru”,
“Philippines”,
“Poland”,
“Portugal”,
“Qatar”,
“Romania”,
“Russia”,
“Rwanda”,
“Saint Kitts and Nevis”,
“Saint Lucia”,
“Saint Vincent”,
“Samoa”,
“San Marino”,
“Sao Tome and Principe”,
“Saudi Arabia”,
“Senegal”,
“Serbia and Montenegro”,
“Seychelles”,
“Sierra Leone”,
“Singapore”,
“Slovakia”,
“Slovenia”,
“Solomon Islands”,
“Somalia”,
“South Africa”,
“Spain”,
“Sri Lanka”,
“Sudan”,
“Suriname”,
“Swaziland”,
“Sweden”,
“Switzerland”,
“Syria”,
“Taiwan”,
“Tajikistan”,
“Tanzania”,
“Thailand”,
“Togo”,
“Tonga”,
“Trinidad and Tobago”,
“Tunisia”,
“Turkey”,
“Turkmenistan”,
“Tuvalu”,
“Uganda”,
“Ukraine”,
“United Arab Emirates”,
“United Kingdom”,
“United States”,
“Uruguay”,
“Uzbekistan”,
“Vanuatu”,
“Vatican City”,
“Venezuela”,
“Vietnam”,
“Yemen”,
“Zambia”,
“Zimbabwe”
);

foreach ( $countries as $country ) {

xprofile_insert_field( array(
‘field_group_id’ => 1,
‘parent_id’ => $country_list_id,
‘type’ => ‘option’,
‘name’ => $country,
‘option_order’ => $i++
));

}

}
}
}
add_action(‘bp_init’, ‘bp_add_custom_country_list’);

[/code]

Then, visit the profile fields settings page at mydomain.com/wp-admin/users.php?page=bp-profile-setup

Once you visited the page, you can see the new field. So you can remove the piece of code in bp-custom.php, don’t let it inside the file.

That’s it! If this post helped you in your project, please share it and mention!

 

Mes derniers tweets

Nouveau short sur Blooness 👉 Vitalité, longévité, perte de poids : le guide ultime des protéines en diète keto-méditerranéenne est en ligne sur Blooness. https://buff.ly/3xxNvpj
https://buff.ly/4cG2GM4

Nouveau short sur Blooness 👉 Le nouveau contenu premium sort dans 48h, et quel contenu ! Ce sera le guide le plus abouti sur la quantité de protéine à consommer en diète kéto-méditerranéenne. A très bientôt !
https://buff.ly/3L2KbFP

New Post: Combo, la meilleure solution pour en finir avec le casse-tête des plannings et de la gestion RH https://buff.ly/4eE6JtT

Charger plus

Ne manquez pas les conseils business par mail

Pas de spam, juste un mail lorsqu'un nouveau contenu est publié.

2 réponses

    1. Hi!
      You need to write a similar code for all cities of the world, but it seems to be complicated as there are a lot of cities to list…

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *

fr_FRFR