{"id":1614,"date":"2018-08-18T00:46:23","date_gmt":"2018-08-17T22:46:23","guid":{"rendered":"https:\/\/tulipemedia.com\/?p=1614"},"modified":"2025-05-15T17:59:43","modified_gmt":"2025-05-15T15:59:43","slug":"buddypress-auto-bulk-import-countries-profile-field","status":"publish","type":"post","link":"https:\/\/tulipemedia.com\/en\/buddypress-auto-bulk-import-countries-profile-field\/","title":{"rendered":"BuddyPress \u2013 Create an automatic country dropdown list in profile field"},"content":{"rendered":"<p>Are you looking for a function that <strong>imports all countries of the world to a BuddyPress profile field<\/strong> called \u00ab\u00a0Country\u00a0\u00bb ? This code is for you.<\/p>\n<p>Consider that you open an international BuddyPress social network and you want let <strong>your users select their country<\/strong>. It&rsquo;s annoying to <strong>manually add countries one by one<\/strong>. The solution is to <strong>import all the countries<\/strong> with this piece of code, <a href=\"https:\/\/gist.github.com\/mywebink\/627ae1b784e5fb109d5c9b0f7c637ab2\" target=\"_blank\" rel=\"noopener\">that you can find on GitHub<\/a>.<\/p>\n<p>Be careful, you have to copy and paste this code in the bp-custom.php file of your BuddyPress install. If you don&rsquo;t have this file, create it and upload it to the \/wp-content\/ folder, and add this code inside it:<\/p>\n<p>[code]<\/p>\n<p>function bp_add_custom_country_list() {<\/p>\n<p>if ( !xprofile_get_field_id_from_name(&lsquo;Country&rsquo;) &amp;&amp; &lsquo;bp-profile-setup&rsquo; == $_GET[&lsquo;page&rsquo;] ) {<\/p>\n<p>$country_list_args = array(<br \/>\n&lsquo;field_group_id&rsquo; =&gt; 1,<br \/>\n&lsquo;name&rsquo; =&gt; &lsquo;Country&rsquo;,<br \/>\n&lsquo;description&rsquo; =&gt; &lsquo;Please select your country&rsquo;,<br \/>\n&lsquo;can_delete&rsquo; =&gt; true,<br \/>\n&lsquo;field_order&rsquo; =&gt; 2,<br \/>\n&lsquo;is_required&rsquo; =&gt; false,<br \/>\n&lsquo;type&rsquo; =&gt; &lsquo;selectbox&rsquo;,<br \/>\n&lsquo;order_by&rsquo; =&gt; &lsquo;custom&rsquo;<\/p>\n<p>);<\/p>\n<p>$country_list_id = xprofile_insert_field( $country_list_args );<\/p>\n<p>if ( $country_list_id ) {<\/p>\n<p>$countries = array(<br \/>\n\u00ab\u00a0Afghanistan\u00a0\u00bb,<br \/>\n\u00ab\u00a0Albania\u00a0\u00bb,<br \/>\n\u00ab\u00a0Algeria\u00a0\u00bb,<br \/>\n\u00ab\u00a0Andorra\u00a0\u00bb,<br \/>\n\u00ab\u00a0Angola\u00a0\u00bb,<br \/>\n\u00ab\u00a0Antigua and Barbuda\u00a0\u00bb,<br \/>\n\u00ab\u00a0Argentina\u00a0\u00bb,<br \/>\n\u00ab\u00a0Armenia\u00a0\u00bb,<br \/>\n\u00ab\u00a0Australia\u00a0\u00bb,<br \/>\n\u00ab\u00a0Austria\u00a0\u00bb,<br \/>\n\u00ab\u00a0Azerbaijan\u00a0\u00bb,<br \/>\n\u00ab\u00a0Bahamas\u00a0\u00bb,<br \/>\n\u00ab\u00a0Bahrain\u00a0\u00bb,<br \/>\n\u00ab\u00a0Bangladesh\u00a0\u00bb,<br \/>\n\u00ab\u00a0Barbados\u00a0\u00bb,<br \/>\n\u00ab\u00a0Belarus\u00a0\u00bb,<br \/>\n\u00ab\u00a0Belgium\u00a0\u00bb,<br \/>\n\u00ab\u00a0Belize\u00a0\u00bb,<br \/>\n\u00ab\u00a0Benin\u00a0\u00bb,<br \/>\n\u00ab\u00a0Bhutan\u00a0\u00bb,<br \/>\n\u00ab\u00a0Bolivia\u00a0\u00bb,<br \/>\n\u00ab\u00a0Bosnia and Herzegovina\u00a0\u00bb,<br \/>\n\u00ab\u00a0Botswana\u00a0\u00bb,<br \/>\n\u00ab\u00a0Brazil\u00a0\u00bb,<br \/>\n\u00ab\u00a0Brunei\u00a0\u00bb,<br \/>\n\u00ab\u00a0Bulgaria\u00a0\u00bb,<br \/>\n\u00ab\u00a0Burkina Faso\u00a0\u00bb,<br \/>\n\u00ab\u00a0Burundi\u00a0\u00bb,<br \/>\n\u00ab\u00a0Cambodia\u00a0\u00bb,<br \/>\n\u00ab\u00a0Cameroon\u00a0\u00bb,<br \/>\n\u00ab\u00a0Canada\u00a0\u00bb,<br \/>\n\u00ab\u00a0Cape Verde\u00a0\u00bb,<br \/>\n\u00ab\u00a0Central African Republic\u00a0\u00bb,<br \/>\n\u00ab\u00a0Chad\u00a0\u00bb,<br \/>\n\u00ab\u00a0Chile\u00a0\u00bb,<br \/>\n\u00ab\u00a0China\u00a0\u00bb,<br \/>\n\u00ab\u00a0Colombi\u00a0\u00bb,<br \/>\n\u00ab\u00a0Comoros\u00a0\u00bb,<br \/>\n\u00ab\u00a0Congo (Brazzaville)\u00a0\u00bb,<br \/>\n\u00ab\u00a0Congo\u00a0\u00bb,<br \/>\n\u00ab\u00a0Costa Rica\u00a0\u00bb,<br \/>\n\u00ab\u00a0Cote d&rsquo;Ivoire\u00a0\u00bb,<br \/>\n\u00ab\u00a0Croatia\u00a0\u00bb,<br \/>\n\u00ab\u00a0Cuba\u00a0\u00bb,<br \/>\n\u00ab\u00a0Cyprus\u00a0\u00bb,<br \/>\n\u00ab\u00a0Czech Republic\u00a0\u00bb,<br \/>\n\u00ab\u00a0Denmark\u00a0\u00bb,<br \/>\n\u00ab\u00a0Djibouti\u00a0\u00bb,<br \/>\n\u00ab\u00a0Dominica\u00a0\u00bb,<br \/>\n\u00ab\u00a0Dominican Republic\u00a0\u00bb,<br \/>\n\u00ab\u00a0East Timor (Timor Timur)\u00a0\u00bb,<br \/>\n\u00ab\u00a0Ecuador\u00a0\u00bb,<br \/>\n\u00ab\u00a0Egypt\u00a0\u00bb,<br \/>\n\u00ab\u00a0El Salvador\u00a0\u00bb,<br \/>\n\u00ab\u00a0Equatorial Guinea\u00a0\u00bb,<br \/>\n\u00ab\u00a0Eritrea\u00a0\u00bb,<br \/>\n\u00ab\u00a0Estonia\u00a0\u00bb,<br \/>\n\u00ab\u00a0Ethiopia\u00a0\u00bb,<br \/>\n\u00ab\u00a0Fiji\u00a0\u00bb,<br \/>\n\u00ab\u00a0Finland\u00a0\u00bb,<br \/>\n\u00ab\u00a0France\u00a0\u00bb,<br \/>\n\u00ab\u00a0Gabon\u00a0\u00bb,<br \/>\n\u00ab\u00a0Gambia, The\u00a0\u00bb,<br \/>\n\u00ab\u00a0Georgia\u00a0\u00bb,<br \/>\n\u00ab\u00a0Germany\u00a0\u00bb,<br \/>\n\u00ab\u00a0Ghana\u00a0\u00bb,<br \/>\n\u00ab\u00a0Greece\u00a0\u00bb,<br \/>\n\u00ab\u00a0Grenada\u00a0\u00bb,<br \/>\n\u00ab\u00a0Guatemala\u00a0\u00bb,<br \/>\n\u00ab\u00a0Guinea\u00a0\u00bb,<br \/>\n\u00ab\u00a0Guinea-Bissau\u00a0\u00bb,<br \/>\n\u00ab\u00a0Guyana\u00a0\u00bb,<br \/>\n\u00ab\u00a0Haiti\u00a0\u00bb,<br \/>\n\u00ab\u00a0Honduras\u00a0\u00bb,<br \/>\n\u00ab\u00a0Hungary\u00a0\u00bb,<br \/>\n\u00ab\u00a0Iceland\u00a0\u00bb,<br \/>\n\u00ab\u00a0India\u00a0\u00bb,<br \/>\n\u00ab\u00a0Indonesia\u00a0\u00bb,<br \/>\n\u00ab\u00a0Iran\u00a0\u00bb,<br \/>\n\u00ab\u00a0Iraq\u00a0\u00bb,<br \/>\n\u00ab\u00a0Ireland\u00a0\u00bb,<br \/>\n\u00ab\u00a0Israel\u00a0\u00bb,<br \/>\n\u00ab\u00a0Italy\u00a0\u00bb,<br \/>\n\u00ab\u00a0Jamaica\u00a0\u00bb,<br \/>\n\u00ab\u00a0Japan\u00a0\u00bb,<br \/>\n\u00ab\u00a0Jordan\u00a0\u00bb,<br \/>\n\u00ab\u00a0Kazakhstan\u00a0\u00bb,<br \/>\n\u00ab\u00a0Kenya\u00a0\u00bb,<br \/>\n\u00ab\u00a0Kiribati\u00a0\u00bb,<br \/>\n\u00ab\u00a0Korea, North\u00a0\u00bb,<br \/>\n\u00ab\u00a0Korea, South\u00a0\u00bb,<br \/>\n\u00ab\u00a0Kuwait\u00a0\u00bb,<br \/>\n\u00ab\u00a0Kyrgyzstan\u00a0\u00bb,<br \/>\n\u00ab\u00a0Laos\u00a0\u00bb,<br \/>\n\u00ab\u00a0Latvia\u00a0\u00bb,<br \/>\n\u00ab\u00a0Lebanon\u00a0\u00bb,<br \/>\n\u00ab\u00a0Lesotho\u00a0\u00bb,<br \/>\n\u00ab\u00a0Liberia\u00a0\u00bb,<br \/>\n\u00ab\u00a0Libya\u00a0\u00bb,<br \/>\n\u00ab\u00a0Liechtenstein\u00a0\u00bb,<br \/>\n\u00ab\u00a0Lithuania\u00a0\u00bb,<br \/>\n\u00ab\u00a0Luxembourg\u00a0\u00bb,<br \/>\n\u00ab\u00a0Macedonia\u00a0\u00bb,<br \/>\n\u00ab\u00a0Madagascar\u00a0\u00bb,<br \/>\n\u00ab\u00a0Malawi\u00a0\u00bb,<br \/>\n\u00ab\u00a0Malaysia\u00a0\u00bb,<br \/>\n\u00ab\u00a0Maldives\u00a0\u00bb,<br \/>\n\u00ab\u00a0Mali\u00a0\u00bb,<br \/>\n\u00ab\u00a0Malta\u00a0\u00bb,<br \/>\n\u00ab\u00a0Marshall Islands\u00a0\u00bb,<br \/>\n\u00ab\u00a0Mauritania\u00a0\u00bb,<br \/>\n\u00ab\u00a0Mauritius\u00a0\u00bb,<br \/>\n\u00ab\u00a0Mexico\u00a0\u00bb,<br \/>\n\u00ab\u00a0Micronesia\u00a0\u00bb,<br \/>\n\u00ab\u00a0Moldova\u00a0\u00bb,<br \/>\n\u00ab\u00a0Monaco\u00a0\u00bb,<br \/>\n\u00ab\u00a0Mongolia\u00a0\u00bb,<br \/>\n\u00ab\u00a0Morocco\u00a0\u00bb,<br \/>\n\u00ab\u00a0Mozambique\u00a0\u00bb,<br \/>\n\u00ab\u00a0Myanmar\u00a0\u00bb,<br \/>\n\u00ab\u00a0Namibia\u00a0\u00bb,<br \/>\n\u00ab\u00a0Nauru\u00a0\u00bb,<br \/>\n\u00ab\u00a0Nepal\u00a0\u00bb,<br \/>\n\u00ab\u00a0Netherlands\u00a0\u00bb,<br \/>\n\u00ab\u00a0New Zealand\u00a0\u00bb,<br \/>\n\u00ab\u00a0Nicaragua\u00a0\u00bb,<br \/>\n\u00ab\u00a0Niger\u00a0\u00bb,<br \/>\n\u00ab\u00a0Nigeria\u00a0\u00bb,<br \/>\n\u00ab\u00a0Norway\u00a0\u00bb,<br \/>\n\u00ab\u00a0Oman\u00a0\u00bb,<br \/>\n\u00ab\u00a0Pakistan\u00a0\u00bb,<br \/>\n\u00ab\u00a0Palau\u00a0\u00bb,<br \/>\n\u00ab\u00a0Panama\u00a0\u00bb,<br \/>\n\u00ab\u00a0Papua New Guinea\u00a0\u00bb,<br \/>\n\u00ab\u00a0Paraguay\u00a0\u00bb,<br \/>\n\u00ab\u00a0Peru\u00a0\u00bb,<br \/>\n\u00ab\u00a0Philippines\u00a0\u00bb,<br \/>\n\u00ab\u00a0Poland\u00a0\u00bb,<br \/>\n\u00ab\u00a0Portugal\u00a0\u00bb,<br \/>\n\u00ab\u00a0Qatar\u00a0\u00bb,<br \/>\n\u00ab\u00a0Romania\u00a0\u00bb,<br \/>\n\u00ab\u00a0Russia\u00a0\u00bb,<br \/>\n\u00ab\u00a0Rwanda\u00a0\u00bb,<br \/>\n\u00ab\u00a0Saint Kitts and Nevis\u00a0\u00bb,<br \/>\n\u00ab\u00a0Saint Lucia\u00a0\u00bb,<br \/>\n\u00ab\u00a0Saint Vincent\u00a0\u00bb,<br \/>\n\u00ab\u00a0Samoa\u00a0\u00bb,<br \/>\n\u00ab\u00a0San Marino\u00a0\u00bb,<br \/>\n\u00ab\u00a0Sao Tome and Principe\u00a0\u00bb,<br \/>\n\u00ab\u00a0Saudi Arabia\u00a0\u00bb,<br \/>\n\u00ab\u00a0Senegal\u00a0\u00bb,<br \/>\n\u00ab\u00a0Serbia and Montenegro\u00a0\u00bb,<br \/>\n\u00ab\u00a0Seychelles\u00a0\u00bb,<br \/>\n\u00ab\u00a0Sierra Leone\u00a0\u00bb,<br \/>\n\u00ab\u00a0Singapore\u00a0\u00bb,<br \/>\n\u00ab\u00a0Slovakia\u00a0\u00bb,<br \/>\n\u00ab\u00a0Slovenia\u00a0\u00bb,<br \/>\n\u00ab\u00a0Solomon Islands\u00a0\u00bb,<br \/>\n\u00ab\u00a0Somalia\u00a0\u00bb,<br \/>\n\u00ab\u00a0South Africa\u00a0\u00bb,<br \/>\n\u00ab\u00a0Spain\u00a0\u00bb,<br \/>\n\u00ab\u00a0Sri Lanka\u00a0\u00bb,<br \/>\n\u00ab\u00a0Sudan\u00a0\u00bb,<br \/>\n\u00ab\u00a0Suriname\u00a0\u00bb,<br \/>\n\u00ab\u00a0Swaziland\u00a0\u00bb,<br \/>\n\u00ab\u00a0Sweden\u00a0\u00bb,<br \/>\n\u00ab\u00a0Switzerland\u00a0\u00bb,<br \/>\n\u00ab\u00a0Syria\u00a0\u00bb,<br \/>\n\u00ab\u00a0Taiwan\u00a0\u00bb,<br \/>\n\u00ab\u00a0Tajikistan\u00a0\u00bb,<br \/>\n\u00ab\u00a0Tanzania\u00a0\u00bb,<br \/>\n\u00ab\u00a0Thailand\u00a0\u00bb,<br \/>\n\u00ab\u00a0Togo\u00a0\u00bb,<br \/>\n\u00ab\u00a0Tonga\u00a0\u00bb,<br \/>\n\u00ab\u00a0Trinidad and Tobago\u00a0\u00bb,<br \/>\n\u00ab\u00a0Tunisia\u00a0\u00bb,<br \/>\n\u00ab\u00a0Turkey\u00a0\u00bb,<br \/>\n\u00ab\u00a0Turkmenistan\u00a0\u00bb,<br \/>\n\u00ab\u00a0Tuvalu\u00a0\u00bb,<br \/>\n\u00ab\u00a0Uganda\u00a0\u00bb,<br \/>\n\u00ab\u00a0Ukraine\u00a0\u00bb,<br \/>\n\u00ab\u00a0United Arab Emirates\u00a0\u00bb,<br \/>\n\u00ab\u00a0United Kingdom\u00a0\u00bb,<br \/>\n\u00ab\u00a0United States\u00a0\u00bb,<br \/>\n\u00ab\u00a0Uruguay\u00a0\u00bb,<br \/>\n\u00ab\u00a0Uzbekistan\u00a0\u00bb,<br \/>\n\u00ab\u00a0Vanuatu\u00a0\u00bb,<br \/>\n\u00ab\u00a0Vatican City\u00a0\u00bb,<br \/>\n\u00ab\u00a0Venezuela\u00a0\u00bb,<br \/>\n\u00ab\u00a0Vietnam\u00a0\u00bb,<br \/>\n\u00ab\u00a0Yemen\u00a0\u00bb,<br \/>\n\u00ab\u00a0Zambia\u00a0\u00bb,<br \/>\n\u00ab\u00a0Zimbabwe\u00a0\u00bb<br \/>\n);<\/p>\n<p>foreach ( $countries as $country ) {<\/p>\n<p>xprofile_insert_field( array(<br \/>\n&lsquo;field_group_id&rsquo; =&gt; 1,<br \/>\n&lsquo;parent_id&rsquo; =&gt; $country_list_id,<br \/>\n&lsquo;type&rsquo; =&gt; &lsquo;option&rsquo;,<br \/>\n&lsquo;name&rsquo; =&gt; $country,<br \/>\n&lsquo;option_order&rsquo; =&gt; $i++<br \/>\n));<\/p>\n<p>}<\/p>\n<p>}<br \/>\n}<br \/>\n}<br \/>\nadd_action(&lsquo;bp_init&rsquo;, &lsquo;bp_add_custom_country_list&rsquo;);<\/p>\n<p>[\/code]<\/p>\n<p>Then, visit the profile fields settings page at mydomain.com\/wp-admin\/users.php?page=bp-profile-setup<\/p>\n<p>Once you visited the page, you can see the new field. So you can remove the piece of code in bp-custom.php, don&rsquo;t let it inside the file.<\/p>\n<p>That&rsquo;s it! If this post helped you in your project, please share it and mention!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Are you looking for a function that imports all countries of the world to a BuddyPress profile field called \u00ab\u00a0Country\u00a0\u00bb ? This code is for you. Consider that you open an international BuddyPress social network and you want let your users select their country. It&rsquo;s annoying to manually add countries one by one. The solution [&hellip;]<\/p>\n","protected":false},"author":17,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[164],"class_list":["post-1614","post","type-post","status-publish","format-standard","hentry","category-buddypress","tag-tips-wordpress"],"_links":{"self":[{"href":"https:\/\/tulipemedia.com\/en\/wp-json\/wp\/v2\/posts\/1614","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tulipemedia.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tulipemedia.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tulipemedia.com\/en\/wp-json\/wp\/v2\/users\/17"}],"replies":[{"embeddable":true,"href":"https:\/\/tulipemedia.com\/en\/wp-json\/wp\/v2\/comments?post=1614"}],"version-history":[{"count":1,"href":"https:\/\/tulipemedia.com\/en\/wp-json\/wp\/v2\/posts\/1614\/revisions"}],"predecessor-version":[{"id":4205,"href":"https:\/\/tulipemedia.com\/en\/wp-json\/wp\/v2\/posts\/1614\/revisions\/4205"}],"wp:attachment":[{"href":"https:\/\/tulipemedia.com\/en\/wp-json\/wp\/v2\/media?parent=1614"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tulipemedia.com\/en\/wp-json\/wp\/v2\/categories?post=1614"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tulipemedia.com\/en\/wp-json\/wp\/v2\/tags?post=1614"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}