Skip to content

Instantly share code, notes, and snippets.

@webzunft
Last active October 28, 2024 08:54
Show Gist options
  • Save webzunft/a63982e4c75ce7408dccd07e7bd78e24 to your computer and use it in GitHub Desktop.
Save webzunft/a63982e4c75ce7408dccd07e7bd78e24 to your computer and use it in GitHub Desktop.
Geo-targeting debug ad for Advanced Ads as seen on https://wpadvancedads.com/manual/geo-targeting-tests-and-issues/
<div style="min-width: 300px; height: auto; padding: 10px; background: #fff; color: #333; text-align: center; border: solid 1px #ddd;">
<p>Your location:</p>
<?php
if( class_exists( 'Advanced_Ads_Geo_Api' ) ) :
$api = Advanced_Ads_Geo_Api::get_instance();
$ip = $api->get_real_IP_address();
$error = false;
$my_country = '';
$my_country_isoCode = '';
$my_continent_code = '';
$my_city = '';
$my_region = '';
$my_lat = '';
$my_long = '';
try {
$reader = $api->get_GeoIP2_city_reader();
// $reader = $api->get_GeoIP2_country_reader();
if( $reader ){
// Look up the IP address
// $record = $reader->country($ip);
$record = $reader->city($ip);
if ( ! empty( $record ) ) {
$my_city = $record->city->name;
$my_country = $record->country->names['en'];
$my_country_isoCode = $record->country->isoCode;
$my_region = isset( $record->subdivisions[0]->name ) ? $record->subdivisions[0]->name : '(unknown region)';
$my_lat = $record->location->latitude;
$my_long = $record->location->longitude;
$my_continent_code = $record->continent->code;
}
} else {
$error = 'Geo Database not found';
}
} catch ( \AdvancedAdsPro\GeoIp2\Exception\AddressNotFoundException $e ) {
$error = $e->getMessage() . ' Maybe you are working on a local or secured environment.';
}
if( $error ){
$current_location = '<span class="advads-error-message">' . $error . '</span>';
} else {
if( ! $my_city ){
$my_city = '(unknown city)';
}
$current_location = $ip . ', ' . $my_country . ', ' . $my_city;
}
echo "your IP: $ip<br/>your continent: $my_continent_code<br/>your country: $my_country<br/>your region: $my_region<br/>your city: $my_city<br/>lat/long: $my_lat / $my_long";
else :
"Geo Targeting module is missing.";
endif;
?>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment