Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save davidmutero/4098e239f6ee3bda440e252e7d356325 to your computer and use it in GitHub Desktop.
Save davidmutero/4098e239f6ee3bda440e252e7d356325 to your computer and use it in GitHub Desktop.
Add A State in PMPro State Dropdown Add On
<?php
/**
* Add a custom state/region to a specific country in the PMPro State Dropdown Add On.
*
* This ensures the new state/region appears in alphabetical order within the country's list.
* Modify `$country_code` and `$custom_state` as needed for your use case.
*
* You can add this recipe to your site by creating a custom plugin
* or using the Code Snippets plugin available for free in the WordPress repository.
* Read this companion article for step-by-step directions on either method:
* https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
*/
function my_pmprosd_add_custom_state_sorted( $states ) {
$country_code = 'US'; // Change to the target country code (e.g., 'CA' for Canada)
$custom_state_code = 'PR'; // Custom state/region abbreviation
$custom_state_name = 'Puerto Rico'; // Custom state/region name
if ( isset( $states[$country_code] ) ) {
$states[$country_code][$custom_state_code] = $custom_state_name;
asort( $states[$country_code] ); // Sort states alphabetically
}
return $states;
}
add_filter( 'pmprosd_states', 'my_pmprosd_add_custom_state_sorted' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment