K
K
KoystrubVS2018-06-16 15:41:06
JavaScript
KoystrubVS, 2018-06-16 15:41:06

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

3 answer(s)
A
Anton Spirin, 2018-06-16
@rockon404

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 .

I
Ivan Kozlov, 2018-06-16
@trampick

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);//Здесь хранятся необходимые данные. Выбирай что нужно - страна, регион, город.

You need to save data using session
This is how you need to initialize sessions in functions.php
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 ();
}

So save and retrieve data
//сохраняем
$_SESSION['city'] = 'Город'; 
//Получаем
if(isset($_SESSION['city'])) {
    $city = $_SESSION['city'];
} else {
    $city= '';
}

R
Roman Kusty, 2021-10-09
@roman_tlt

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 question

Ask a Question

731 491 924 answers to any question