Skip to content

Instantly share code, notes, and snippets.

@webzunft
Last active Nov 11, 2019
Embed
What would you like to do?
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_city = '';
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', AAGT_SLUG );
}
} catch (\GeoIp2\Exception\AddressNotFoundException $e) {
$error = $e->getMessage() . ' ' . __( "Maybe you are working on a local or secured environment.", AAGT_SLUG );
}
if( $error ){
$current_location = '<span class="advads-error-message">' . $error . '</span>';
} else {
if( ! $my_city ){
$my_city = __( '(unknown city)', AAGT_SLUG );
}
$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 :
_e( "Geo Targeting add-on is missing." );
endif;
?>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment