I need to do some very basic geo-targeting for a WordPress site.
Something like:
<?php if ( visitor is from usa ): ?>
<?php get_footer('us'); ?>
<?php else: ?>
<?php get_footer('other-countries'); ?>
<?php endif; ?>
Till now I used the GEO service provided by ip-api.com. You can see the code I used till now, below.
The problem is that the php unserialize is now deprecated and extremely slow. Ip-api suggest to
use JSON instead. But I am a total newbie and I don't know how to achieve the same results with JSON. Can someone please help me?
I do know that I should use the following code:
$.getJSON( '//ip-api.com/json?callback=?', function( data ) {
console.log( JSON.stringify( data, null, 2 ) );
});
But I need help to form the complete final code.
I know there are various plug-ins out there, but I think they are overkill for the very simple geo targeting I need.
What I have tried:
<?php $ip = $_SERVER['REMOTE_ADDR'];
$query = @unserialize(file_get_contents('http://ip-api.com/php/' . $ip));
$geo = $query['countryCode'];
if( $query['countryCode'] == 'US') : ?>
DO THIS
<?php else: ?>
DO THAT
<?php endif ?>