Is it possible to output pages depending on ip in cities and towns of Belarus?
- oneTry sypexgeo.net/ru Must work for Belarus. Not a plugin, but very simple to use. - KAGG Design
- Used for Russia, works well. - KAGG Design
- This will allow me to display exactly the wordpress pages by geolocation? or can I only use pages? - Victor Timoshkov
- The code allows you to display anything and anything. If you own WordPress sufficiently. From the question it is not clear what exactly you are trying to do. Clarify the question, I will answer. - KAGG Design
- I'm going to display content in the php code of the wordpress site, as well as in the pages and lists of the admin panel, depending on the IP address. - Victor Timoshkov
|
1 answer
Place the SxGeo.php and SxGeoCity.dat files from https://sypexgeo.net/ in your theme folder. Add this code to functions.php:
global $user_ip, $user_city, $SxGeo; require_once( trailingslashit( get_stylesheet_directory() ) . 'SxGeo.php' ); $SxGeo = new SxGeo( trailingslashit( get_stylesheet_directory() ) . 'SxGeoCity.dat' ); if (isset($_SERVER["HTTP_CF_CONNECTING_IP"])) { $user_ip = $_SERVER["HTTP_CF_CONNECTING_IP"]; } else { $user_ip = $_SERVER['REMOTE_ADDR']; } $user_city = $SxGeo->getCityFull( $user_ip ); This code connects the Sypex Geo library and locates by ip address. HTTP_CF_CONNECTING_IP needed if your site works via Cloudflare.
To determine the country, region and city, use the global variable $user_city in the right place as follows:
if ( 'Россия' === $user_city['country']['name_ru'] ) //... if ( 'Санкт-Петербург' === $user_city['city']['name_ru'] ) //... if ( 'Ленинградская область' === $user_city['region']['name_ru'] ) //... Examples are taken from the working code.
- It definitely suits me. Be sure to use your example. - Victor Timoshkov
- Let me know later whether it works in Belarus or not. Interesting. - KAGG Design
- Yes, good, necessary - Victor Timoshkov
|