Answer the question
In order to leave comments, you need to log in
Saving the city like on Avito?
How can I implement saving the city by the selected user? On Avito or sbagry.com, when choosing a city, the site remembers the choice and then until you choose another one, you will be taken to the main page of this city.
Interested in any way, in general it is interesting whether there is a solution for wordpress.
Answer the question
In order to leave comments, you need to log in
Can be stored in cookie or localstorage . On first access can be determined using navigator.geolocation . If the user declined the request for access to geodata, you can determine the location by ip .
So you can determine the current city by ip
$server = $_SERVER['REMOTE_ADDR'];
$rrs = file_get_contents("http://api.sypexgeo.net/json/" . $server);
$obj = json_decode($rrs);
print_r($obj);//Здесь хранятся необходимые данные. Выбирай что нужно - страна, регион, город.
add_action('init', 'myStartSession', 1);
add_action('wp_logout', 'myEndSession');
add_action('wp_login', 'myEndSession');
function myStartSession() {
if(!session_id()) {
session_start();
}
}
function myEndSession() {
session_destroy ();
}
//сохраняем
$_SESSION['city'] = 'Город';
//Получаем
if(isset($_SESSION['city'])) {
$city = $_SESSION['city'];
} else {
$city= '';
}
If we consider back-end solutions, then the free plugin WT Geotargeting will help you . With its help, the site can implement the choice of the city and the display of various information with reference to the city.
When you select a city, the value is stored in the cookie and you do not need to select the city when you navigate through the pages or visit again.
The plugin has an extended paid version of WT Geotargeting Pro , which makes it easier to manage a large number of cities, you can set up subdomains, there is WooCommerce support and much more.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question