Support » Plugin: WP Engine GeoTarget » How To Create Full Page Redirects Using The Plugin

  • Hi,
    For certain pages I have where I just need a few components or text changing, them the shortcode works fine.

    For other pages I need completely different landing pages depending on the country.

    So for example https://goproposal.com/pricing-uk or https://goproposal.com/pricing-usa

    I found the code below in one of the other threads but wasn’t clear if that was for the entire site or whether it could be applied to specific pages too.

    Cheers…

    function country_geo_redirect() {
    $country = getenv('HTTP_GEOIP_COUNTRY_CODE');
    if ( $country == "US" ) {
    wp_redirect( 'http://us.domain.com';, 301 );
    exit;
    } else if ( $country == "GB" ) {
    wp_redirect( 'http://gb.domain.com';, 301 );
    exit;
    }
    }
    add_action('init', country_geo_redirect');
Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Contributor Hoover

    (@ryanshoover)

    Hey @thejamesashford!

    Something like this code should work for your needs.

    <?php
    
    /**
     * Redirect any pricing pages to my country's pricing page
     */
    function redirect_pricing_page() {
    
        $path = $_SERVER['REQUEST_URI'];
    
        // If the path doesn't contain /pricing-, abort
        if ( ! stristr( $path, '/pricing-' ) ) {
            return;
        }
    
        // If the GeoIp class doesn't exist, abort
        if ( ! class_exists( 'WPEngine\GeoIp' ) ) {
            return;
        }
    	
        $geo = WPEngine\GeoIp::instance();
    
    	if ( 'US' === $geo->country() && '/pricing-usa/' !== $path ) {
    		wp_redirect( site_url( 'pricing-usa' ), 301 );
    		exit;
    	}
    
        if ( 'GB' === $geo->country() && '/pricing-uk/' !== $path ) {
    		wp_redirect( site_url( 'pricing-uk' ), 301 );
    		exit;
    	}
    }
    
    add_action( 'wp', 'redirect_pricing_page' );
    • This reply was modified 2 years, 1 month ago by  Hoover. Reason: fix a bug in the code snippet

    That’s awesome hoover thank you…. now for the REALLY stupid question…… where do I put it?

    I’m not a developer.

    Many thanks

    Jasonian

    (@jasonian)

    You would put it in your theme’s functions.php file, or you would use it as a plugin.

    If you are not using a child theme, I recommend it. It makes changes like this possible without fear of them being erased upon updating your theme.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How To Create Full Page Redirects Using The Plugin’ is closed to new replies.