0

I have two sites, one for the US which is on the root of the domain .com and I have a copy of the site in a directory called /uk aimed towards UK customers.

Here is the code I have set up that works fine when I use a test blank php page, I'm using with a geoplugin class.

<?php ob_start();?>
<?php require_once('geoplugin.class.php');$geoplugin = new geoPlugin(); ?>
<?php $country = $geoplugin->locate();?>

        <?php
        $countryName= $geoplugin->countryName ;
        $ukCountry = 'http://www.mywebsite.com/uk/';
        $mainSite = 'http://www.mywebsite.com/';


//UK 
         if ($countryName =="United Kingdom")
          {

                    header('Location: '.$ukCountry);
          }else{

                    header('Location: '.$mainSite);

?>

I am connecting to the above page via an include statement and works fine as a test.

But what I want to happen is when a visitor will comes to the main site and then check the country of origin and if the country is United Kingdom then forwards to the UK version.

I want to install this on a Wordpress site but when I place the 'include' statement I'm getting the error

Warning: Cannot modify header information - headers already sent by

I've read up a lot about output buffering and I I've tried a number of things to get it to work but I can't get it to forward it correctly.

I know there is a chance of a infinite loop with the above code but I will remove the else statement if to stop this.

Basically I want the the site to check the country is the UK if so forward to the UK site and if not just load the page normally.

Any suggestions welcomed.

JohnnyQ
  • 484
  • 10
  • 23
  • have you tried built in function wp_redirect? http://codex.wordpress.org/Function_Reference/wp_redirect In your case, please read http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php – FrEaKmAn Dec 31 '14 at 17:18
  • 2
    Another solution is to use apache rewrite rules and geoIP module – ceadreak Dec 31 '14 at 17:22
  • Where are you including it on your WordPress site? – jeroen Dec 31 '14 at 17:24
  • 1
    have a look at http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php – Utkarsh Dixit Dec 31 '14 at 17:28
  • Please post the full error – Utkarsh Dixit Dec 31 '14 at 17:29
  • Yes I have looked at the stackoverflow questions, and the javascript workaround does work but it seems to make the site very sluggish and was hoping for a better solution. the apache rules and the geoIP module looks good but unsure if I can install it on my servers hosting. – JohnnyQ Dec 31 '14 at 19:47

1 Answers1

0

First of all you can use Maxmind for Geo Targeting. Then create plug_in in word press and use HOOK system in work press to hock your code with the init Hook .

SMSM
  • 1,509
  • 3
  • 19
  • 34
  • I'm already using a plugin that works fine it's the implementation to wordpress that is causing the error. – JohnnyQ Dec 31 '14 at 19:48