Skip to content

Instantly share code, notes, and snippets.

@timersys
Created July 9, 2021 13:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timersys/03bdd89e5ff7cf05cdc8e09be5914da9 to your computer and use it in GitHub Desktop.
Save timersys/03bdd89e5ff7cf05cdc8e09be5914da9 to your computer and use it in GitHub Desktop.
Set custom data based on urls on GeotargetingWP
<?php
class GeotWP_Url_Location {
public function __construct() {
add_filter( 'geot/cancel_query', [ $this, 'set_custom_data' ] );
}
/**
* Check if url is set and modify data
*/
function set_custom_data() {
// if no cookie or not a valid city continue with request to API
if ( ! isset( $_GET['geot_country'] ) ) ) ) {
return false;
}
$country = esc_attr($_GET['geot_country']);
setcookie( 'geot_rocket_country', apply_filters( 'geot_rocket_country', $country ), 0, '/' );
setcookie( 'geot_country', apply_filters( 'geot_rocket_country', $country ), 0, '/' );
$data = [
'country' => $country,
'country_iso' => $country,
'state' => '',
'state_iso' => '',
'city' => '',
'zip' => '',
];
// return formatted object to the plugin
return $this->formatter($data);
}
private function formatter( $data ) {
$state = new \stdClass;
$state->names = [ $data['state'] ];
$state->iso_code = $data['state_iso'];
$country = new \stdClass;
$country->names = [ $data['country'] ];
$country->iso_code = $data['country_iso'];
$continent = new \stdClass;
$continent->names = '';
$city = new \stdClass;
$city->names = [ $data['city'] ];
$city->zip = $data['zip'];
$geolocation = new \stdClass();
$geolocation->accuracy_radius = '';
$geolocation->longitude = '';
$geolocation->latitude = '';
$geolocation->time_zone = '';
return (object) [
'country' => $country,
'city' => $city,
'state' => $state,
'continent' => $continent,
'geolocation' => $geolocation,
];
}
}
add_action( 'plugins_loaded', function () {
new GeotWP_Url_Location();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment