Forked from strangerstudios/pmpro-restrict-countries.php
Last active
September 13, 2017 12:05
-
-
Save andrewlimaza/3911e8f19b967461d31d23d0c785c09f to your computer and use it in GitHub Desktop.
Allow certain countries from signing up for certain levels.
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 | |
// Add this code to your PMPro Customizations Plugin - https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/ | |
function my_init() | |
{ | |
global $allowed_countries; | |
//specify the countries allowed to signup. The key is the level id. | |
$allowed_countries = array( | |
1 => array('FR', 'IT'), | |
2 => array('IT'), | |
); | |
} | |
add_action('init', 'my_init'); | |
function my_pmpro_registration_checks($value) | |
{ | |
global $allowed_countries, $pmpro_msg, $pmpro_msgt; | |
$country = $_REQUEST['bcountry']; | |
$level_id = $_REQUEST['level']; | |
//only check if the level has restrictions and country is allowed. | |
if( array_key_exists($level_id, $allowed_countries) && in_array($country, $allowed_countries[$level_id])) | |
{ | |
//do nothing. | |
}else{ | |
$pmpro_msg = "Your country of residence is not permitted to register for this level."; | |
$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 $allowed_countries, $pmpro_countries; | |
if( array_key_exists($level->id, $allowed_countries )) | |
{ | |
$text = $text." This level can only be purchased if you reside in the following countries: "; | |
//code for commas | |
$i = 1; | |
foreach($allowed_countries[$level->id] as $country) | |
{ | |
$text = $text. $pmpro_countries[$country]; | |
if($i != count($allowed_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