Description
This guide provides instructions on how to create a custom BuddyPress xProfile field for selecting countries from a predefined list. The field will be added to the default profile field group, and users will be able to choose their country from a dropdown menu.
Code Explanation
Function: bp_add_custom_country_list()
This function creates a new xProfile field in BuddyPress if it doesn’t already exist and adds a list of country options to that field.
-
Check for Existing Field:
The code checks if a field with the nameCountryexists in the xProfile fields. If the field does not exist, it proceeds to create the field and populate it with the list of countries. -
Define the Country List:
A predefined array of country names is included. These countries will be added as options under the newCountryfield. - Insert the Country Field:
The xProfile field forCountryis created with thexprofile_insert_field()function, and each country is inserted as an option under this field.
Code Implementation
function bp_add_custom_country_list() {
// Check if the 'Country' field exists, if not, create it
if ( !xprofile_get_field_id_from_name('Country') && 'bp-profile-setup' == $_GET['page'] ) {
// Define the country field attributes
$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'
);
// Insert the new field
$country_list_id = xprofile_insert_field( $country_list_args );
// Check if the field was successfully created
if ( $country_list_id ) {
// Define the list of countries
$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"
);
// Insert each country as an option in the new 'Country' field
$i = 1;
foreach ( $countries as $country ) {
xprofile_insert_field( array(
'field_group_id' => 1,
'parent_id' => $country_list_id,
'type' => 'option',
'name' => $country,
'option_order' => $i++
));
}
}
}
}
// Hook into BuddyPress init action
add_action('bp_init', 'bp_add_custom_country_list');
Steps to Implement:
-
Open Your WordPress Admin Dashboard
Go to your WordPress installation where BuddyPress is active. -
Add the Code to bp-custom.php
Add the provided code to your BuddyPressbp-custom.phpfile or your theme’sfunctions.phpfile. -
Access Profile Setup
Navigate to BuddyPress Profile Setup. TheCountryfield will be created automatically with the list of countries available for users to select. - Test the Field
Visit a user profile or registration page and verify that theCountryfield appears in the profile section.
What the Code Does:
- Creates a custom xProfile field called
Country. - Adds a list of country names as selectable options for users.
- Ensures the
Countryfield is added during BuddyPress profile setup, only if the field does not already exist.
