Skip to content

Instantly share code, notes, and snippets.

@timersys
Last active February 9, 2016 17:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timersys/da9c4523e8429020e0f8 to your computer and use it in GitHub Desktop.
Save timersys/da9c4523e8429020e0f8 to your computer and use it in GitHub Desktop.
Redirect users to same site depending on language
<?php
/**
* Create Custom Redirects
* @link https://timersys.com/geotargeting/
*
* @wordpress-plugin
* Plugin Name: GeoTargeting Pro Custom Redirects
* Plugin URI: https://timersys.com/geotargeting/
* Description: Geo Targeting custom redirects
* Version: 1.0.0
* Author: Timersys
* Author URI: https://timersys.com/geotargeting/
*/
function geot_redirects() {
global $geot;
// if main plugin is not enabled return
if( ! function_exists( 'geot_target' ) )
return;
// if search engine exit
if( $geot->functions->isSearchEngine() )
return;
// if is admin user / backend or login page return
if( is_admin() || checkUrl('wp-login') )
return;
// grab wpml language of current user
$lang = ICL_LANGUAGE_CODE;
// grab country code of current user
$user_code = geot_country_code();
// if you want to debug insert ?geot_debug=true to your url
if( isset( $_GET['geot_debug'] ) ) {
var_dump( $lang );
var_dump( $user_code );
die();
}
if($user_code == "UK" && $lang != "gb"){
wp_safe_redirect("https://yourwebsite/gb/");die();
}
if($user_code == "US" && $lang != "en"){
wp_safe_redirect("https://yourwebsite/en/");die();
}
if($user_code == "FR" && $lang != "fr"){
wp_safe_redirect("https://yourwebsite/fr/");die();
}
/**
* You can also use any other function of the API to create redirects
* https://timersys.com/geotargeting/docs/functions-api/
*/
}
// if you need to use template tags such as is_front_page use template_redirect hook instead of init
add_action( 'init', 'geot_redirects');
function checkUrl( $prefix ) {
return (strpos( $_SERVER['REQUEST_URI'], $prefix ) !== false );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment