prashantcs053
7/3/2017 - 1:27 PM

check region by ip

check region by ip

function is_nsw_region() {
    $ip = "122.106.130.21";
    if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        $_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_X_FORWARDED_FOR'];
        unset($_SERVER['HTTP_X_FORWARDED_FOR']);
    }
    $ip = $_SERVER["REMOTE_ADDR"];
    $ipurl = "http://ipinfo.io/{$ip}/json";
    $data = file_get_contents($ipurl);
    if ($data != "") {
        $ipdata = json_decode($data, true);
        $region = $ipdata["region"];

        $region = strtolower($region);
        if (strpos($region, "new south wales") !== false || strpos($region, "nsw") !== false) {
            return '1';
        } elseif (strpos($region, "victoria") !== false || strpos($region, "vic") !== false) {
            return '2';
        }
         return false;
    }
   
}

add_action("wp", "check_region");

function check_region() {
    if (is_404() && isset($_GET['check_region']) && $_GET['check_region'] == 'yes') {
        echo $is_region = is_nsw_region();
        exit;
    }
}