How to add a US State dropdown field

Iain Poulson avatar
Iain Poulson Last updated on March 6, 2021

To add a dropdown field that contains the US States, first add a new dropdown field to a field group and edit the ‘Unique meta key’ setting to 
field_us_state.

Then create a new file ‘wpum-field-state.php’ in wp-content/mu-plugins (you might need to create the directory), and copy and paste the following code:

<?php

add_filter( 'get_wpum_field_metadata', 'my_wpum_state_data', 10, 3 );

function my_wpum_state_data ( $check, $object_id, $meta_key ) {
	if ( 'dropdown_options' !== $meta_key ) {
		return $check;
	}

	$user_meta_key = WPUM()->field_meta->get_meta( $object_id, 'user_meta_key', true );
	if ( $user_meta_key !== 'wpum_field_us_state' ) {
		return $check;
	}

	$states = array(
		'AL' => 'Alabama',
		'AK' => 'Alaska',
		'AZ' => 'Arizona',
		'AR' => 'Arkansas',
		'CA' => 'California',
		'CO' => 'Colorado',
		'CT' => 'Connecticut',
		'DE' => 'Delaware',
		'DC' => 'District Of Columbia',
		'FL' => 'Florida',
		'GA' => 'Georgia',
		'HI' => 'Hawaii',
		'ID' => 'Idaho',
		'IL' => 'Illinois',
		'IN' => 'Indiana',
		'IA' => 'Iowa',
		'KS' => 'Kansas',
		'KY' => 'Kentucky',
		'LA' => 'Louisiana',
		'ME' => 'Maine',
		'MD' => 'Maryland',
		'MA' => 'Massachusetts',
		'MI' => 'Michigan',
		'MN' => 'Minnesota',
		'MS' => 'Mississippi',
		'MO' => 'Missouri',
		'MT' => 'Montana',
		'NE' => 'Nebraska',
		'NV' => 'Nevada',
		'NH' => 'New Hampshire',
		'NJ' => 'New Jersey',
		'NM' => 'New Mexico',
		'NY' => 'New York',
		'NC' => 'North Carolina',
		'ND' => 'North Dakota',
		'OH' => 'Ohio',
		'OK' => 'Oklahoma',
		'OR' => 'Oregon',
		'PA' => 'Pennsylvania',
		'RI' => 'Rhode Island',
		'SC' => 'South Carolina',
		'SD' => 'South Dakota',
		'TN' => 'Tennessee',
		'TX' => 'Texas',
		'UT' => 'Utah',
		'VT' => 'Vermont',
		'VA' => 'Virginia',
		'WA' => 'Washington',
		'WV' => 'West Virginia',
		'WI' => 'Wisconsin',
		'WY' => 'Wyoming',
	);

	$data = array();
	foreach ( $states as $state_abbrev => $state ) {
		$data[] = array( 'value' => $state_abbrev, 'label' => $state );
	}

	return array( $data );
}
WP User Manager logo

Get Started with the Best WordPress Membership Plugin Today

Connect, Manage and Build your Membership Site

Need more help?

If you still didn't find an answer you can always contact us

Submit a ticket