Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kimwhite/9271849fce6385b49666b9598ca2078f to your computer and use it in GitHub Desktop.
Save kimwhite/9271849fce6385b49666b9598ca2078f to your computer and use it in GitHub Desktop.
Show specific Country in Billing address Drowndown
<?php
/**
* This recipe is an example of how to keep only specific countries in the dropdown.
* and remove all other countries from the "Country" dropdown select.
*
* The PMPro default country is "US" (United States), to set
* your custom default country, please refer to this article.
* @link https://www.paidmembershipspro.com/change-the-default-country-of-your-membership-website/
*
* For available countries and their shortcode see:
* @link https://github.com/strangerstudios/paid-memberships-pro/blob/master/includes/countries.php
*
* NOTE: This recipe is not intended for or compatible with the PMPro State Dropdowns Add On.
*
* 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_pmpro_countries_show_only_selected( $countries ) {
// Define the allowed countries
$allowed_countries = array(
'CH', 'RS', 'JP', 'CA', 'US', 'KR', 'AT', 'BE', 'BG', 'HR', 'CY', 'CZ', 'DK', 'EE', 'FI', 'FR', 'DE', 'GR', 'HU', 'IE', 'IT', 'LV', 'LT', 'LU', 'MT', 'NL', 'PL', 'PT', 'RO', 'SK', 'SI', 'ES', 'SE'
);
// Loop through and remove any country not in the allowed list
foreach ( $countries as $country_code => $country_name ) {
if ( ! in_array( $country_code, $allowed_countries, true ) ) {
unset( $countries[ $country_code ] );
}
}
return $countries;
}
add_filter( 'pmpro_countries', 'my_pmpro_countries_show_only_selected' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment