Created
December 12, 2016 12:29
-
-
Save timersys/687d884bbe4537d3ac742430f3b404d1 to your computer and use it in GitHub Desktop.
Redirect sitepress WPML + Geotargeting
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Redirect WPML + Geotargeting shared by thomsonreuters.com | |
*/ | |
function redirect_ip_country(){ | |
global $wpdb, $post, $sitepress; | |
if(isset($post) && !$_COOKIE['icl_ip_to_country_check']){ | |
// Grab the language code from the GEOT e.g. de | |
$lang_code_from_ip = geot_country_code(); | |
$lang_code_from_ip = strtolower($lang_code_from_ip); | |
// Get the current post, page language code | |
$_post = wpml_get_language_information( $post->ID ); | |
$post_language_code = substr( $_post['locale'], 0, 2 ); | |
// If they are different redirect by language code from ip | |
if($sitepress->get_current_language() != $lang_code_from_ip){ | |
$url = $sitepress->convert_url(get_permalink($post->ID), $lang_code_from_ip); | |
} | |
//Now lets set a browser cookie and do the redirect | |
setcookie('icl_ip_to_country_check','checked'); | |
wp_safe_redirect( $url ); | |
} | |
} | |
add_action('template_redirect', 'redirect_ip_country'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment