Skip to content

Instantly share code, notes, and snippets.

@dwanjuki
Last active August 4, 2025 07:57
Show Gist options
  • Save dwanjuki/42d5e1e54e51ca832e73c3d198524194 to your computer and use it in GitHub Desktop.
Save dwanjuki/42d5e1e54e51ca832e73c3d198524194 to your computer and use it in GitHub Desktop.
Set a default state for the State Dropdowns Add On.
<?php
/**
* Set a default Mailing Address state (State Dropdowns Add On)
*
* Setting a default country:
* https://github.com/strangerstudios/pmpro-snippets-library/blob/dev/checkout/set-pmpro-default-country.php
*
* 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_default_state() {
// Bail if State Dropdowns is not active.
if ( ! function_exists( 'pmprosd_states' ) ) {
return;
}
// Bail if not on the checkout page.
if ( ! pmpro_is_checkout() ) {
return;
}
// Set state to Queensland on page load if country is Australia and a state is not already set.
?>
<script>
jQuery( window ).on( 'load', function () {
var state = jQuery( '#pmpro_sstate' ).val();
if ( ! state ) {
var country = jQuery( '#pmpro_scountry' ).val();
if ( country === 'AU' ) {
jQuery( '#pmpro_sstate' ).val( 'QLD' );
}
}
} );
</script>
<?php
}
add_action( 'wp_footer', 'my_pmprosd_default_state' );
@dwanjuki
Copy link
Author

dwanjuki commented Aug 4, 2025

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment