How to develop a wordpress homepage that changes according to locale

Asked

Viewed 50 times

-1

I need to create a site, in wordpress, that the home recognizes the location through IP. Example:

In Rio de Janeiro the IP is XXXXXX, in which case the home page would be X In São Paulo the IP is yyyyyy, the home page would be another, in case Y

You can do it?

  • There is no way. Do the same as the telephony sites, put a modal asking the state where the user lives.

  • Try to see something thus

1 answer

0


Yes. And it can be done by both the client and the server part.

For this you will have to have a geolocation service, in this case I used the ipinfo.io that offers both paid and free services with the limitation of 50,000 monthly accesses(if example stop working will be due to this limit).

From the client the example is simple, just make a request to http://ipinfo.io equipped with callback that accepts a parameter that will receive a JSON with the geographic location information.

In that link information about the service API is available http://ipinfo.io.

Example stand alone simulating client geolocation in Javascript

$.get("http://ipinfo.io", function (r) {
    $("#ip").html("IP: " + r.ip);
    $("#localização").html("Localização: " + r.city + ", " + r.region + ", " + r.country);    
}, "json");
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="ip"></div>
<div id="localização"></div>

From the server part what changes is that instead of passing a callback what will be informed is the IP address of the client that stored in $_SERVER\['REMOTE_ADDR'\] and the response must be decoded into an associative array by json_decode( string $json [, bool $assoc ] ) : mixed

Example of the server side in PHP

<?php

  if (isset($_SERVER['REMOTE_ADDR'])) {
     $r = json_decode ( 
        file_get_contents('http://ipinfo.io/'.$_SERVER['REMOTE_ADDR'].'?'), true
     );
     echo "Localização: " . $r['city'] . ", " . $r['region'] .  ", " . $r['country'];    
  } else {
     echo "Endereço IP não disponível para localização.";
  }

?>

Remembering that the part of the server should take into account that the client may be behind a proxy and other techniques would be necessary to detect the proxy and obtain the client’s IP, techniques that go beyond the scope of the question.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.