Click here to Skip to main content
15,889,863 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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 ?>
Posted
Updated 22-Aug-19 1:12am

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900