Documentation > Developers > Filter Examples > Add US States to the Country List

Add US States to the Country List

Add one of the following snippets via the Code Snippets plugin or create a custom plugin.

There are two ways to do it, please choose one of the following snippets.

1. Replace all of the countries with US states

This will replace all of the countries that will be available in the tax settings with the US states set in this snippet.

/**
 * Add US States to the country list.
 */
function my_picu_add_us_states_to_country_list( $countries ) {
	// US States
	$us_states = [
		'us_al' => 'Alabama',
		'us_ak' => 'Alaska',
		'us_as' => 'American Samoa',
		'us_az' => 'Arizona',
		'us_ar' => 'Arkansas',
		'us_ca' => 'California',
		'us_co' => 'Colorado',
		'us_ct' => 'Connecticut',
		'us_de' => 'Delaware',
		'us_dc' => 'District of Columbia',
		'us_fl' => 'Florida',
		'us_ga' => 'Georgia',
		'us_gu' => 'Guam',
		'us_hi' => 'Hawaii',
		'us_id' => 'Idaho',
		'us_il' => 'Illinois',
		'us_in' => 'Indiana',
		'us_ia' => 'Iowa',
		'us_ks' => 'Kansas',
		'us_ky' => 'Kentucky',
		'us_la' => 'Louisiana',
		'us_me' => 'Maine',
		'us_md' => 'Maryland',
		'us_ma' => 'Massachusetts',
		'us_mi' => 'Michigan',
		'us_mn' => 'Minnesota',
		'us_ms' => 'Mississippi',
		'us_mo' => 'Missouri',
		'us_mt' => 'Montana',
		'us_ne' => 'Nebraska',
		'us_nv' => 'Nevada',
		'us_nh' => 'New Hampshire',
		'us_nj' => 'New Jersey',
		'us_nm' => 'New Mexico',
		'us_ny' => 'New York',
		'us_nc' => 'North Carolina',
		'us_nd' => 'North Dakota',
		'us_mp' => 'Northern Mariana Islands',
		'us_oh' => 'Ohio',
		'us_ok' => 'Oklahoma',
		'us_or' => 'Oregon',
		'us_pa' => 'Pennsylvania',
		'us_pr' => 'Puerto Rico',
		'us_ri' => 'Rhode Island',
		'us_sc' => 'South Carolina',
		'us_sd' => 'South Dakota',
		'us_tn' => 'Tennessee',
		'us_tx' => 'Texas',
		'us_vi' => 'U.S. Virgin Islands',
		'us_um' => 'U.S. Minor Outlying Islands',
		'us_ut' => 'Utah',
		'us_vt' => 'Vermont',
		'us_va' => 'Virginia',
		'us_wa' => 'Washington',
		'us_wv' => 'West Virginia',
		'us_wi' => 'Wisconsin',
		'us_wy' => 'Wyoming'
	];

	$countries = $us_states;

	return $countries;
}

add_filter( 'picu_countries', 'my_picu_add_us_states_to_country_list' );Code language: PHP (php)

2. Add US states to the country list

This will add the US states to the existing list of countries that is available in the tax settings.

/**
 * Add US States to the country list.
 */
function my_picu_add_us_states_to_country_list( $countries ) {
	// US States
	$us_states = [
		'us_al' => 'Alabama',
		'us_ak' => 'Alaska',
		'us_as' => 'American Samoa',
		'us_az' => 'Arizona',
		'us_ar' => 'Arkansas',
		'us_ca' => 'California',
		'us_co' => 'Colorado',
		'us_ct' => 'Connecticut',
		'us_de' => 'Delaware',
		'us_dc' => 'District of Columbia',
		'us_fl' => 'Florida',
		'us_ga' => 'Georgia',
		'us_gu' => 'Guam',
		'us_hi' => 'Hawaii',
		'us_id' => 'Idaho',
		'us_il' => 'Illinois',
		'us_in' => 'Indiana',
		'us_ia' => 'Iowa',
		'us_ks' => 'Kansas',
		'us_ky' => 'Kentucky',
		'us_la' => 'Louisiana',
		'us_me' => 'Maine',
		'us_md' => 'Maryland',
		'us_ma' => 'Massachusetts',
		'us_mi' => 'Michigan',
		'us_mn' => 'Minnesota',
		'us_ms' => 'Mississippi',
		'us_mo' => 'Missouri',
		'us_mt' => 'Montana',
		'us_ne' => 'Nebraska',
		'us_nv' => 'Nevada',
		'us_nh' => 'New Hampshire',
		'us_nj' => 'New Jersey',
		'us_nm' => 'New Mexico',
		'us_ny' => 'New York',
		'us_nc' => 'North Carolina',
		'us_nd' => 'North Dakota',
		'us_mp' => 'Northern Mariana Islands',
		'us_oh' => 'Ohio',
		'us_ok' => 'Oklahoma',
		'us_or' => 'Oregon',
		'us_pa' => 'Pennsylvania',
		'us_pr' => 'Puerto Rico',
		'us_ri' => 'Rhode Island',
		'us_sc' => 'South Carolina',
		'us_sd' => 'South Dakota',
		'us_tn' => 'Tennessee',
		'us_tx' => 'Texas',
		'us_vi' => 'U.S. Virgin Islands',
		'us_um' => 'U.S. Minor Outlying Islands',
		'us_ut' => 'Utah',
		'us_vt' => 'Vermont',
		'us_va' => 'Virginia',
		'us_wa' => 'Washington',
		'us_wv' => 'West Virginia',
		'us_wi' => 'Wisconsin',
		'us_wy' => 'Wyoming'
	];

	$keys = array_keys( $countries );
	$index = array_search( 'us', $keys );

	if ( $index === false ) {
		// Append to the end if the key is not found
		return array_merge( $countries, $us_states );
	}

	$position = $index + 1;
	$before = array_slice( $countries, 0, $position, true );
	$after = array_slice( $countries, $position, null, true );

	return $before + $us_states + $after;
}

add_filter( 'picu_countries', 'my_picu_add_us_states_to_country_list' );Code language: PHP (php)

Need help?

If you couldn’t find what you were looking for and need more assistance, please get in touch with us directly and we’re happy to help.