Is there an easy way to echo the users city?

I want to get the city the user is, I know geolocation isn’t completely precise but I would like to get it so I show it as a suggestion to the users.

Is there an easy way to get the users location? With easy I just mean idk copy/paste some code and get the city he is in. I’m using WordPress.

Related posts

Leave a Reply

1 comment

  1. One of the most reliable provider provides that service for free, e.g.

      http://api.hostip.info/get_html.php?ip=12.215.42.19
    

    Result would be

    Country: UNITED STATES (US)
    City: Sugar Grove, IL
    IP: 12.215.42.19

    From PHP

      $ip = '12.215.42.19';
      $info = file_get_contents("http://api.hostip.info/get_html.php?ip=$ip");
    

    Then with some text processing you can get the city easily.