Skip to content
Benjamin Pick edited this page Mar 17, 2017 · 32 revisions

(Feel free to add your own examples on this page.)

Basic Use

$userInfo = geoip_detect2_get_info_from_current_ip();
if ($userInfo->country->isoCode == 'DE')
    echo 'Hallo! Schön dass Sie hier sind!';

CSS Use

Hide/Show text only if visitor from Germany:

In your CSS file:

.geoip { display: none !important; }
.geoip-country-UK .geoip-show-UK { display: block !important; }
.geoip-country-DE .geoip-show-DE { display: block !important; }

.geoip-hide { display: block !important; }
.geoip-country-UK .geoip-hide-UK { display: none !important; }
.geoip-country-DE .geoip-hide-DE { display: none !important; }

In your HTML (e.g. in the post content, when switching the editor to the HTML mode):

<div class="geoip geoip-show-DE">
This text is shown only in Germany
</div>
<div class="geoip-hide geoip-hide-DE">
This text is hidden only in Germany
</div>

You need to enable the option Add a country-specific CSS class to the <body>-Tag to make this work.

Shortcode Examples

[geoip_detect2 property="country"] -> Germany
[geoip_detect2 property="country" lang="de"] -> Deutschland
[geoip_detect2 property="country.isoCode"] -> de
[geoip_detect2 property="city"] -> Frankfurt/Main
[geoip_detect2 property="mostSpecificSubdivision"] -> Hesse
[geoip_detect2 property="mostSpecificSubdivision.isoCode"] -> HE
[geoip_detect2 property="location.longitude"] -> 9.202
[geoip_detect2 property="location.latitude"] -> 48.9296
[geoip_detect2 property="location.timeZone"] -> Europe/Berlin
[geoip_detect2 property="continent"] -> Europe
[geoip_detect2 property="continent.code"] -> EU
[geoip_detect2 property="invalid_or_empty_property_name" default="default value"] -> default value

Complete Solutions

Add the city name to the hash of the URL

add_action('wp_head', 'geoip_add_city_to_hash', 5);
function geoip_add_city_to_hash(){
	$userInfo = geoip_detect2_get_info_from_current_ip();
	$city = $userInfo->city->name;
	if ($city) {
?>
<script>
window.location.hash = <?php echo json_encode('#' . $city) ?>;
</script>
<?php
	}
}

Redirect depending on country

add_action('template_redirect', 'geoip_redirect', 5);
function geoip_redirect(){
	if (is_admin())
		return;

	// This condition prevents a redirect loop:
	// Redirect only if the home page is called. Change this condition to the specific page or URL you need.
	if (!is_home())
		return;

	if (!function_exists('geoip_detect2_get_info_from_current_ip'))
		return;

	$userInfo = geoip_detect2_get_info_from_current_ip();
	$countryCode = $userInfo->country->isoCode;
	switch ($countryCode) {
		case 'DE':
			$url = '/germany';
			break;
		case 'US':
			$url = '/usa';
			break;
		default:
			$url = '';
	}
	if ($url) {
		wp_redirect(get_site_url(null, $url));
		exit;
	}
}

Calculate distance from a known location

/**
 * Calculates the great-circle distance between two points, with
 * the Haversine formula. 
 * @param float $latitudeFrom Latitude of start point in [deg decimal]
 * @param float $longitudeFrom Longitude of start point in [deg decimal]
 * @param float $latitudeTo Latitude of target point in [deg decimal]
 * @param float $longitudeTo Longitude of target point in [deg decimal]
 * @param float $earthRadius Mean earth radius in [km]
 * @return float Distance between points in [km] (same as earthRadius)
 * @see https://stackoverflow.com/a/10054282
 */
function haversineGreatCircleDistance(
  $latitudeFrom, $longitudeFrom, $latitudeTo, $longitudeTo, $earthRadius = 6371)
{
  // convert from degrees to radians
  $latFrom = deg2rad($latitudeFrom);
  $lonFrom = deg2rad($longitudeFrom);
  $latTo = deg2rad($latitudeTo);
  $lonTo = deg2rad($longitudeTo);

  $latDelta = $latTo - $latFrom;
  $lonDelta = $lonTo - $lonFrom;

  $angle = 2 * asin(sqrt(pow(sin($latDelta / 2), 2) +
    cos($latFrom) * cos($latTo) * pow(sin($lonDelta / 2), 2)));
  return $angle * $earthRadius;
}

// Los Angeles
$location['lat'] = 37.6293;
$location['lon'] = -122.1163;

$myLocation = $location; // Change if default Location should be something else
$record = geoip_detect2_get_info_from_current_ip();
if ($record->location->longitude) {
  $myLocation['lon'] = $record->location->longitude;
  $myLocation['lat'] = $record->location->latitude;  
}

$distance = haversineGreatCircleDistance($location['lat'], $location['lon'], $myLocation['lat'], $myLocation['lon']); // Returns distance in km. If you need a different unit, change the $earthRadius

Use Maxmind Precise only for customers in Germany.

(Since 2.7.0)

$record = geoip_detect2_get_info_from_current_ip();
if ($record->country->isoCode == 'DE') {
	$record = geoip_detect2_get_info_from_current_ip(null, array('source' => 'precise'));
}

For this to work, you need to first activate the precise datasource, enter the credentials. Then activate 'Maxmind automatic update' (or any other source) to use as default source.

Clone this wiki locally