Last active
September 28, 2021 01:21
-
-
Save Garconis/19c5a44b19b8578f1883c87201fa5866 to your computer and use it in GitHub Desktop.
BuddyPress | xprofile add states as user dropdown field
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
If you are using BP 2.1+, this will insert a State selectbox. | |
Add the function to bp-custom.php and then visit .../wp-admin/users.php?page=bp-profile-setup | |
Remove this function after the field is created. | |
*/ | |
function bp_add_custom_state_list() { | |
if ( !xprofile_get_field_id_from_name('State') && 'bp-profile-setup' == $_GET['page'] ) { | |
$state_list_args = array( | |
'field_group_id' => 1, | |
'name' => 'State', | |
'description' => 'Please select your state', | |
'can_delete' => true, | |
'field_order' => 2, | |
'is_required' => false, | |
'type' => 'selectbox', | |
'order_by' => 'custom' | |
); | |
$state_list_id = xprofile_insert_field( $state_list_args ); | |
if ( $state_list_id ) { | |
$states = array( | |
"Alabama", | |
"Alaska", | |
"Arizona", | |
"Arkansas", | |
"California", | |
"Colorado", | |
"Connecticut", | |
"Delaware", | |
"District of Columbia", | |
"Florida", | |
"Georgia", | |
"Hawaii", | |
"Idaho", | |
"Illinois", | |
"Indiana", | |
"Iowa", | |
"Kansas", | |
"Kentucky", | |
"Louisiana", | |
"Maine", | |
"Maryland", | |
"Massachusetts", | |
"Michigan", | |
"Minnesota", | |
"Mississippi", | |
"Missouri", | |
"Montana", | |
"Nebraska", | |
"Nevada", | |
"New Hampshire", | |
"New Jersey", | |
"New Mexico", | |
"New York", | |
"North Carolina", | |
"North Dakota", | |
"Ohio", | |
"Oklahoma", | |
"Oregon", | |
"Pennsylvania", | |
"Rhode Island", | |
"South Carolina", | |
"South Dakota", | |
"Tennessee", | |
"Texas", | |
"Utah", | |
"Vermont", | |
"Virginia", | |
"Washington", | |
"West Virginia", | |
"Wisconsin", | |
"Wyoming" | |
); | |
foreach ( $states as $state ) { | |
xprofile_insert_field( array( | |
'field_group_id' => 1, | |
'parent_id' => $state_list_id, | |
'type' => 'option', | |
'name' => $state, | |
'option_order' => $i++ | |
)); | |
} | |
} | |
} | |
} | |
add_action('bp_init', 'bp_add_custom_state_list'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment