Ok, I want a page from my wordpress website to be geo-localised, meaning I want to exclude people from country X to be able to read the page but automatically be redirected to the home page
I wrote the following code in functions.php , but somehow it does not trigger a redirect. In the example I want to redirect visitors from Belgium to the homepage when they land on a specific url.
function redirect_belgian_users() {
if (!isset($_SERVER["REMOTE_ADDR"])) {
error_log("No IP-address detected.");
return;
}
// Establish the IP-adres
$user_ip = $_SERVER["HTTP_X_FORWARDED_FOR"] ?? $_SERVER["REMOTE_ADDR"];
error_log("Geo-block function triggered for IP: " . $user_ip);
// Vraag de geo-locatie op
$response = wp_remote_get("/{$user_ip}/json");
if (is_wp_error($response)) {
error_log("Geo-block API error: " . $response->get_error_message());
return;
}
$geo_data = json_decode(wp_remote_retrieve_body($response), true);
if (empty($geo_data["country"])) {
error_log("Geo-block: No country information received.");
return;
}
error_log("Geo-block: IP {$user_ip} is in {$geo_data["country"]}");
if ($geo_data["country"] !== "BE") {
error_log("Visitors not from Belgium. No action needed.");
return;
}
// Check of de gebruiker zich op de specifieke pagina bevindt
$current_url_path = parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH);
$restricted_url_path = "an-url-I-want-to-geo-block";
error_log("Huidige pagina: {$current_url_path}");
if ($current_url_path !== $restricted_url_path) {
error_log("User is not on the geo-blocked page. No redirect.");
return;
}
// Redirect uitvoeren
error_log("Redirecting Belgian visitor from {$current_url_path}");
wp_safe_redirect(home_url());
exit;
}
add_action("template_redirect", "redirect_belgian_users");
My debug report said: [22-Mar-2025 11:03:51 UTC] Geo-block function triggered for IP: 217.113.194.181 [22-Mar-2025 11:03:51 UTC] Geo-block: No land information received.
I tried to further rewrite the code but it does not seem to work.
Ok, I want a page from my wordpress website to be geo-localised, meaning I want to exclude people from country X to be able to read the page but automatically be redirected to the home page
I wrote the following code in functions.php , but somehow it does not trigger a redirect. In the example I want to redirect visitors from Belgium to the homepage when they land on a specific url.
function redirect_belgian_users() {
if (!isset($_SERVER["REMOTE_ADDR"])) {
error_log("No IP-address detected.");
return;
}
// Establish the IP-adres
$user_ip = $_SERVER["HTTP_X_FORWARDED_FOR"] ?? $_SERVER["REMOTE_ADDR"];
error_log("Geo-block function triggered for IP: " . $user_ip);
// Vraag de geo-locatie op
$response = wp_remote_get("http://ipinfo.io/{$user_ip}/json");
if (is_wp_error($response)) {
error_log("Geo-block API error: " . $response->get_error_message());
return;
}
$geo_data = json_decode(wp_remote_retrieve_body($response), true);
if (empty($geo_data["country"])) {
error_log("Geo-block: No country information received.");
return;
}
error_log("Geo-block: IP {$user_ip} is in {$geo_data["country"]}");
if ($geo_data["country"] !== "BE") {
error_log("Visitors not from Belgium. No action needed.");
return;
}
// Check of de gebruiker zich op de specifieke pagina bevindt
$current_url_path = parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH);
$restricted_url_path = "an-url-I-want-to-geo-block";
error_log("Huidige pagina: {$current_url_path}");
if ($current_url_path !== $restricted_url_path) {
error_log("User is not on the geo-blocked page. No redirect.");
return;
}
// Redirect uitvoeren
error_log("Redirecting Belgian visitor from {$current_url_path}");
wp_safe_redirect(home_url());
exit;
}
add_action("template_redirect", "redirect_belgian_users");
My debug report said: [22-Mar-2025 11:03:51 UTC] Geo-block function triggered for IP: 217.113.194.181 [22-Mar-2025 11:03:51 UTC] Geo-block: No land information received.
I tried to further rewrite the code but it does not seem to work.
Share Improve this question asked Mar 22 at 16:34 Bjorn JoossenBjorn Joossen 211 silver badge4 bronze badges 4 |1 Answer
Reset to default 0I didn't check your code. If you want to redirect to a custom IP, you can use htaccess.The code negatively impacts site performance. sample code in htaccess:
RewriteEngine On
RewriteCond %{REMOTE_ADDR} ^123\.456\.789\.000$
RewriteRule ^(.*)$ /custom-page [R=301,L]
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744308395a4567829.html
$geo_data
as a whole when it's returned from the remote service? Did it contain what you expect? Have you tried connecting from a few different IP addressed to see if it makes any difference? Bear in mind that most of these free IP location services are just big databases, often with member-contributed content. They often aren't complete, so some IP ranges might not return a result, or might be inaccurate. – ADyson Commented Mar 23 at 7:52