最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - Why can't I geo-localise a page from my website with functions.php in Wordpress? - Stack Overflow

programmeradmin4浏览0评论

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 IP address is really not a reliable indicator of the country someone is from – ADyson Commented Mar 22 at 16:48
  • Thanks for the reaction, Dyson, It does not have to be 100% waterproof, but I did see this a lot as a European when I visited American website. Now it would be helpful if we could do this on a page level with a specific url in stead of the entire site. – Bjorn Joossen Commented Mar 23 at 6:35
  • 1 OK. So have you done any debugging? When you test it, what was in $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
  • Indeed, I have to access a database with IP's in order to actually sort it out. That is why my script was going nowhere. And yes, unless I want to create one, I looked for one and found this plugin (IP2Location Redirection) that actually allows me to geo-localise domains and/or urls. Thank you for leading me to the right path, Dyson! – Bjorn Joossen Commented Mar 23 at 10:03
Add a comment  | 

1 Answer 1

Reset to default 0

I 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]
发布评论

评论列表(0)

  1. 暂无评论