Skip to content

Instantly share code, notes, and snippets.

  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save craigedmonds/19f3f7a512c57c33caf2f1e41b31c8b8 to your computer and use it in GitHub Desktop.
Wordpress Redirect User based on Country and Page ID
<?php
############################
// Block a country from accessing a certain wordpress page
// Put this script into your functions.php
// Author: craig@123marbella.com
// Date: 21/9/2017
############################
//define some page id's that you want to block
$array_of_page_ids_to_block = array(
"999",
"888",
);
//define a list of countries you want to block
$array_of_country_codes_to_block = array(
"NO",
"ES",
);
//define the url you want to redirect to
$blocked_redirect_page = "https://www.somedomain.com/";
//check to see if this page is in the list of pages to block
if(in_array(get_the_ID(), $array_of_page_ids_to_block)){
//grab the country code from ipinfo.io
$country_code = file_get_contents("https://ipinfo.io/{$_SERVER['REMOTE_ADDR']}/country");
//ipinfo adds a whitespace to the end of the response so filter it out
$country_code = trim($country_code);
//check now to see if the country is in the block list
if(in_array($country_code, $array_of_country_codes_to_block)){
//redirect the user to the $redirect_page
wp_redirect ($blocked_redirect_page);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment