Skip to content

Instantly share code, notes, and snippets.

@michaelbeil
Forked from kimwhite/pmpro-restrict-countries.php
Last active February 14, 2023 23:27
Show Gist options
  • Save michaelbeil/2fcb0aeef4758b7cb2a388e603e27794 to your computer and use it in GitHub Desktop.
Save michaelbeil/2fcb0aeef4758b7cb2a388e603e27794 to your computer and use it in GitHub Desktop.
Only allow for the countries in the $restricted_countries array
<?php
/**
* Only sell to specific countries by adding in the 'allowed' countries' in
* the $restricted_countries array.
* change messages on line 35 and 51 for your site.
*
* 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/
* Country Codes - https://www.iban.com/country-codes
*/
function my_init()
{
global $restricted_countries;
//Only allow for the countries in the $restricted_countries array
$restricted_countries = array(
1 => array('GB'), // Number is the level, then Add Country Codes, as many as true
// 2 => array('US'), // copy for each level
);
}
add_action('init', 'my_init');
function my_pmpro_registration_checks($value)
{
global $restricted_countries, $pmpro_msg, $pmpro_msgt;
$country = $_REQUEST['bcountry'];
$level_id = $_REQUEST['level'];
//only check if the level has restrictions
if(array_key_exists($level_id, $restricted_countries) != in_array($country, $restricted_countries[$level_id]))
{
$pmpro_msg = "You must be a resident of Great Britain to register.";
$pmpro_msgt = "pmpro_error";
$value = false;
}
return $value;
}
add_filter("pmpro_registration_checks", "my_pmpro_registration_checks");
function my_pmpro_level_expiration_text($text, $level)
{
global $restricted_countries, $pmpro_countries;
if(array_key_exists($level->id, $restricted_countries ))
{
$text = $text." Only people from Great Britain can sign up here! ";
//code for commas
$i = 1;
foreach($restricted_countries[$level->id] as $country)
{
$text = $text. $pmpro_countries[$country];
if($i != count($restricted_countries[$level->id]))
$text = $text. ", ";
$i++;
}
}
return $text;
}
add_filter("pmpro_level_expiration_text", "my_pmpro_level_expiration_text", 10, 2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment