GeoIP with Wordpress?

mlektra

Banned
Jan 8, 2008
57
1
0
Canada
Hey guys, just want to know if somebody know how to install MaxMind GeoIP php code in wordpress. I've been following this tuto to achieve it but it didn't worked in my posts and/or my pages. But when i left the print command (print_r) in the code, it showed me my own geoip information in the header :

PHP:
include(”geoip.inc”;);
include(”geoipcity.inc”;);
include(”geoipregionvars.php”;);
 $gi = geoip_open(”;http://www.hiringcontractorsnow.com/GeoT/GeoLiteCity.dat”;, GEOIP_STANDARD);
 $rsGeoData = geoip_record_by_addr($gi, $_SERVER['REMOTE_ADDR']);
 echo(”;”;);
print_r($rsGeoData);
echo(”;”;);
 geoip_close($gi);
Is somebody have a clever way to install it on a wordpress blog?

thanks
 


How did you try to render the $rsGeoData values that it didn't work? Did you get error messages?

I'm shooting in the dark because you're not very specific on the problem, but could it be that you didn't render (ie output) the data on the page at all? The $rsGeoData is an object that contains the data you need. print_r dumps the keys and values contained and shows you the structure. You say that the object is not empty, so that part works - now you just need to output the info in the right places.

From the site you link to:

Code:
[country_code] => US
[country_code3] => USA
[country_name] => United States
[region] => NY
[city] => Buffalo
[postal_code] => 14217
[latitude] => 42.9761
[longitude] => -78.8727
[area_code] => 716
[dma_code] => 514

To render a value, you'd type something like

Code:
You are located in <?php echo $rsGeoData->city; ?>, <?php echo $rsGeoData->region; ?>.

If this is not the issue, then it would help if you could explain the problem a little better.
 
How did you try to render the $rsGeoData values that it didn't work? Did you get error messages?

If this is not the issue, then it would help if you could explain the problem a little better.

Yeah, I forgot to say that I call the function with the same string than :

You are located in <?php echo $rsGeoData->city; ?>, <?php echo $rsGeoData->region; ?>.

When I do it it gives me :

You are located in , .

There's no error, it just don't show it. I'm actually able to put other php code in my posts/pages and it works well. As you can see, it's like he get that php code, but just can render what i am asking for.

But if I leave the "print_r($rsGeoData);" string in the code in my header.php it will render that data :

[country_code] => US
[country_code3] => USA
[country_name] => United States
[region] => NY
[city] => Buffalo
[postal_code] => 14217
[latitude] => 42.9761
[longitude] => -78.8727
[area_code] => 716
[dma_code] => 514


on the top of my page. But horizontally.

Even if I keep the print command it won't show the data on my posts.

I remove it because I dont want to see that on the top of my site.

I tried a lot of things. Putting the code in on page and after insert the render string (You are located in <?php echo $rsGeoData->city; ?>, <?php echo $rsGeoData->region; ?>.)

Didnt worked.
I'm really puzzled.
 
Ok. Try setting $rsGeoData to global before you echo it.

Code:
global $rsGeoData;

This doesn't go in header.php but right before you echo it.
 
Oh...just like that :

I installed a landing page rotator (rotator.php) with a script that I found in the Prosper202 site :

Code:
<?

//Tracking202 Landing Page Rotation Script

//landing pages filenames, theses will be rotated between eachother

//theses landing pages must be in the same DIRECTORY as this file

//you can add as many landing pages here as you like

$landingpage[1] = 'http://www.whiteteethdigest.com/';

$landingpage[2] = 'http://www.whiteteethdigest.com/lp2';

$lpNumber = rand(1,count($landingpage));

//include the landing page

include_once($landingpage[$lpNumber]);

//terminate script

die();

?>
The thing is that when I am rotating my 2 pages in Wordpress, it works (it rotates) but the thing is the city shown is not anymore the one from the user (in my case Montreal) but it shows the city of where my server is located (Houston) !! Do you know how we can fix this ?
 
My guess is that this is because you are doing a http request in rotator.php. Try including not on the http level, but using the filesystem:

Code:
$landingpage[1] = 'path/to/lp1.php';

Also, rotator.php should be located on the same domain.
 
Ok but since my landing pages are "pages" in wordpress, they are not physically a file per se. How I am suppose to do that since my 2 pages are :

$landingpage[1] = 'http://www.whiteteethdigest.com/';

$landingpage[2] = 'http://www.whiteteethdigest.com/lp2';
 
Right. I forgot it's WP you are using this with lol. I would then modify the page.php file. From what you say, I am assuming you use pages, not posts. I'm also assuming that you have set up WP to show a sticky page on the homepage since your first LP was the main URL.

- move the rotation.php script to the top of page.php in your theme
- replace the URLs for the two array values with the page IDs of the two landing pages, like so:

Code:
$landingpage[1] = 8;
$landingpage[2] = 12;

8 and 12 would be the page IDs for the two different WP pages.

- remove the include_once line from the rotation script
- remove the die() line from the rotation script
- instead, add in this line before the normal Wordpress loop on the page

Code:
query_posts('page_id='.$landingpage[$lpNumber]);

- and after the Wordpress loop, at the bottom of the page, you have to reset the query with

Code:
wp_reset_query();

This way it will all be on the same URL (your root url) but it will display different pages at random. I haven't tested it but it should work.
 
[drunk] I just may scream at the top of my lungs, learn some PHP already! It'll pay for itself![/drunk]
 
Right. I forgot it's WP you are using this with lol. I would then modify the page.php file. From what you say, I am assuming you use pages, not posts. I'm also assuming that you have set up WP to show a sticky page on the homepage since your first LP was the main URL.

In fact no. The first page is not a sticky. But the permalink is the root domain : White Teeth Digest

I'm a little bit worried by the fact of putting my "rotator" page in a page that is in the core structure of my theme?

And what do I do If I want to have multiple rotator page like for different set of landing pages? Like rotator1.php that serve lp1.php and lp2.php for Yahoo traffic. And rotator2.php that serve lp3.php and lp4.php for Bing traffic, etc...

@Rage9
"I just may scream at the top of my lungs"

Give yourself a rest buddy :2drinkspit:
 
A theme is not the big mystery you make it out to be. Just make a backup before following my steps above and you should be fine. To add more LPs, follow the outline that came with the rotator script.

If you're too worried to edit a little bit of code, look for a plugin (I don't know of one though) or pay someone to do this for you.
 
If you're too worried to edit a little bit of code, look for a plugin (I don't know of one though) or pay someone to do this for you.

No, not at all, I don't mind edit some code. I do backup all the time..;)

But, what I understand is the rotator file is now page.php ? the thing is, as I explained in my last post, that I will need various rotator files to send people from different traffic.

Now, what I understand is that I will send all people to page.php ?

Please clarify
 
Lol. Fine. There are different ways to send different traffic sources elsewhere but why don't you add an URL parameter to all your ads' destination links, like source=yahoo, and then call the rotation script for that traffic source.

- You don't need to put the rotation code right in page.php but you can include it from page.php
- You can have multiple rotationXYZ.php for multiple traffic sources
- You can parse $_GET['source'] before including rotationXYZ.php and include whatever rotation script you want
- And finally, switch out index.php with all of the above code that you had in page.php, and make the homepage of your Wordpress blog display the page that your rotation script chooses to display. Basically copy-paste page.php into index.php.

You won't find out if this works unless you try it out.