Creating a Custom Field for US States in Buddypress

Buddypress US state custom field

Over the years, I have played around with the WordPress BuddyPress plugin for a handful of projects.

BuddyPress can do so much, but something I had been wanting to do (but couldn’t find an option for) was add a custom field called “State” with a drop-down list of states in the US.

Searching around, it didn’t appear as though anyone had shared a solution for this yet.

So, I rolled up my sleeves and created a bit of code to hopefully save others some time.

Note: This solution was created a year or two ago, so I’d recommend testing it out first.

First, open up “bp-custom.php.”

After that, add the following code:

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'      => false,
		       '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(
            "District of Columbia",
            "New Hampshire",
            "New Jersey",
            "New Mexico",
            "New York",
            "North Carolina",
            "North Dakota",
            "Rhode Island",
            "South Carolina",
            "South Dakota",
            "West Virginia",
			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');

All done! Now, whenever you view /wp-admin/users.php?page=bp-profile-setup, this custom field will appear along with a list of states in the USA.

Written by Hannah Wright