noptin_locate_ip_address_alt( $ip_address )
Alternate for geolocating an ip address.
Return Return
(bool|array)
Source Source
File: includes/functions.php
function noptin_locate_ip_address_alt( $ip_address ) { // Ensure that we have an IP. if ( empty( $ip_address ) ) { return false; } // Maybe fetch from cache. $transient_name = md5( "noptin_geolocation_cache_$ip_address" ); if ( get_transient( $transient_name ) ) { return get_transient( $transient_name ); } // Retrieve API key. $geo = wp_remote_get( esc_url( "http://ip-api.com/json/$ip_address?fields=9978329" ) ); if ( is_wp_error( $geo ) ) { return false; } // Prepare the data. $geo = json_decode( wp_remote_retrieve_body( $geo ) ); if ( empty( $geo ) || 'success' !== $geo->status ) { log_noptin_message( __( 'Error fetching GeoLocation information.', 'newsletter-optin-box' ) ); return false; } $location = array( 'continent' => $geo->continent, 'country' => $geo->country, 'state' => $geo->regionName, 'city' => $geo->city, 'latitude' => $geo->lat, 'longitude' => $geo->lon, 'time zone' => $geo->timezone, 'currency' => $geo->currency, ); $location = noptin_clean( $location ); set_transient( $transient_name, $location, HOUR_IN_SECONDS ); return $location; }
Expand full source code Collapse full source code View on GitHub
Changelog Changelog
Version | Description |
---|---|
1.3.1 | Introduced. |