Skip to content

Instantly share code, notes, and snippets.

@ryanshoover
Last active August 15, 2017 15:52
Show Gist options
  • Select an option

  • Save ryanshoover/5ef76d5a9d8396804ff9563892bf5eb1 to your computer and use it in GitHub Desktop.

Select an option

Save ryanshoover/5ef76d5a9d8396804ff9563892bf5eb1 to your computer and use it in GitHub Desktop.
GeoIP-based pricing pages
<?php
// !! Don't paste the <?php line above !!
/**
* 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( '/pricing-', $path ) ) {
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_safe_redirect( site_url( 'pricing-usa' ), 301 );
exit;
}
if ( 'GB' === $geo->country() && '/pricing-uk/' !== $path ) {
wp_safe_redirect( site_url( 'pricing-uk' ), 301 );
exit;
}
}
add_action( 'wp', 'redirect_pricing_page' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment