A Cozmoslabs Product
Documentation / Developer Knowledge Base / Custom Fields / Select field with US states

Select field with US states

You might be interested in adding a Select field with the US states to your form but don’t want to through the process of writing them all down.
This can be achieved through a normal Select field and some code which automatically populates it.

First create a Select field with the meta_name us_states and then add the code that will follow to your website.

Notes:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
add_filter('wppb_select_options_array', 'wppbc_us_states_select_options', 20, 5);
function wppbc_us_states_select_options($options, $field, $form_location, $user_id, $request_data) {
	if ($field['meta-name'] != 'us_states')
		return $options;
 
	$states = array_keys(wppbc_get_us_states());
 
	return $states;
}
 
add_filter('wppb_select_labels_array', 'wppbc_us_states_select_labels', 20, 5);
function wppbc_us_states_select_labels($labels, $field, $form_location, $user_id, $request_data) {
	if ($field['meta-name'] != 'us_states')
		return $labels;
 
	$states = array_values(wppbc_get_us_states());
 
	return $states;
}
 
add_filter('wppb_extra_select_option', 'wppbc_us_states_dummy_option', 20, 3);
function wppbc_us_states_dummy_option($return, $field, $item_title) {
	if ($field['meta-name'] != 'us_states')
		return $return;
 
	return '<option value="" selected>Select an option</option>';
}
 
function wppbc_get_us_states() {
	$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',
	);
 
	return $states;
}

The Ultimate Membership Bundle

Combine the power of Profile Builder with Paid Member Subscriptions to set up user registration, memberships, and recurring revenue.

Get 25% off with the bundle

Help & Support

We’re here to help you every step of the way.

Open a Support Ticket