Created
April 25, 2025 10:09
-
-
Save ipokkel/f9dcbae02131bcf232d9371dbda7e641 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Require user to reside in specific countries to register for membership levels. | |
* | |
* This recipe restricts membership checkout based on the user's country of residence. | |
* | |
* === CONFIGURATION INSTRUCTIONS === | |
* | |
* 1. ALLOWED COUNTRIES (Line 29) | |
* - Edit the $allowed_countries array in the my_pmpro_allowed_countries() function | |
* - Add two-letter country codes for allowed countries (e.g., 'US', 'CA', 'UK') | |
* - Example: $allowed_countries = array( 'US', 'CA', 'UK' ); | |
* | |
* 2. ERROR MESSAGE (Line 42) | |
* - Customize the error message shown when a user from a disallowed country attempts checkout | |
* - Modify the first parameter in the pmpro_setMessage() function | |
* | |
* 3. LEVEL DESCRIPTION MESSAGE (Line 56) | |
* - Edit the text that appears on the membership level descriptions | |
* - Modify the string that begins with "This membership can only be purchased..." | |
* | |
* 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_allowed_countries() { | |
// Specify the countries allowed for all membership levels. | |
$allowed_countries = array( 'GB', 'US' ); // Add all countries that should be allowed. | |
return $allowed_countries; | |
} | |
function my_pmpro_registration_checks_allowed_countries( $continue_registration ) { | |
// Get the allowed countries. | |
$allowed_countries = my_pmpro_allowed_countries(); | |
$country = isset( $_REQUEST['bcountry'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['bcountry'] ) ) : ''; // Get the country used for billing. | |
// Check if the user's country is in the allowed list. | |
if ( ! empty( $allowed_countries ) && ! in_array( $country, $allowed_countries ) ) { | |
pmpro_setMessage( 'Your country of residence is not permitted to register for this membership.', 'pmpro_error' ); // Set error message. | |
return false; | |
} | |
return $continue_registration; | |
} | |
add_filter( 'pmpro_registration_checks', 'my_pmpro_registration_checks_allowed_countries' ); | |
function my_pmpro_level_expiration_text_allowed_countries( $text, $level ) { | |
global $pmpro_countries; | |
$allowed_countries = my_pmpro_allowed_countries(); | |
if ( ! empty( $allowed_countries ) ) { | |
// Set your custom message here. | |
$text = $text . ' This membership can only be purchased if you reside in the following countries: '; | |
// Format the list of countries with proper grammar. | |
$country_names = array(); | |
foreach ( $allowed_countries as $country ) { | |
$country_names[] = $pmpro_countries[ $country ]; | |
} | |
if ( count( $country_names ) === 1 ) { | |
$text .= $country_names[0]; | |
} elseif ( count( $country_names ) === 2 ) { | |
$text .= $country_names[0] . ' and ' . $country_names[1]; | |
} else { | |
$last = array_pop( $country_names ); | |
$text .= implode( ', ', $country_names ) . ', and ' . $last; | |
} | |
$text .= '.'; | |
} | |
return $text; | |
} | |
add_filter( 'pmpro_level_expiration_text', 'my_pmpro_level_expiration_text_allowed_countries', 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To restrict by country on a per level basis use https://gist.github.com/ipokkel/aa2ee03957524281137fc185b0ae8e01