1. How to show content to all countries except one or two?
Simple use exclude option. Eg: [geot exclude=”US”] Will show to everyone except US users[/geot]
2. How to debug the plugin?, it’s always showing same country to me
To avoid waste of resource we cache the user geolocation once it’s generated. So in order to refresh the result you need to clean your browser session. In order to debug without the need to clear session every time you can add to your wp-config.php the following: define(‘GEOT_DEBUG’, true);
3. How to redirect users on the same site ?
On the plugin settings page you can create redirections based on country from your site to a external one.
If you need to create inner redirections you have to use the API provided and do some custom code. We create this two tiny plugins as examples. You can place these files inside the wp-content/plugins/ folder and activate them as a normal plugin, just search for the “Geo Targeting custom redirects” plugin.
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 | |
/** | |
* 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 ); | |
} |
Another example redirecting users just from home page
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 | |
use Jaybizzle\CrawlerDetect\CrawlerDetect; | |
/** | |
* Create Custom Redirects from home page only | |
* @link http://wp.timersys.com/geotargeting/ | |
* | |
* @wordpress-plugin | |
* Plugin Name: GeoTargeting Pro Custom Redirects | |
* Plugin URI: http://wp.timersys.com/geotargeting/ | |
* Description: Geo Targeting custom redirects | |
* Version: 1.0.0 | |
* Author: Timersys | |
* Author URI: http://wp.timersys.com/geotargeting/ | |
*/ | |
add_action( 'template_redirect', 'geot_custom_redirects'); | |
function geot_custom_redirects() { | |
// if main plugin is not enabled return | |
if( ! function_exists( 'geot_target' ) ) | |
return; | |
// check for crawlers | |
$CD = new CrawlerDetect(); | |
if( $CD->isCrawler() ) | |
return; | |
// if is admin user or backend return | |
//if( is_admin() ) | |
// return; | |
// if not in home page return | |
if( ! is_front_page() ) | |
return; | |
if( geot_target( 'US' ) ) { | |
wp_redirect( 'http://domain.com/us-home-page' ); | |
die(); | |
} | |
if( geot_target('', 'Other Region name you created' ) ) { | |
wp_redirect( 'http://domain.com/ca-home-page' ); | |
die(); | |
} | |
} |
4. How to use Geotargeting with Advanced Custom fields plugin?
The answer to this question is a bit long to appear in here but we wrote a nice post about it!
5. How to display different menus based on user country?
Check the following gist that shows how to edit your menu location templates with geotargeting functions.
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 | |
/** | |
* Show different WordPress menus depending on user country | |
* by using Geotargeting plugin API functions | |
* https://timersys.com/plugins/geotargeting-pro/ | |
**/ | |
if( function_exists( 'geot_target' ) ) { | |
//USA menu | |
if( geot_target( 'US' ) ) { | |
wp_nav_menu( array( | |
'menu' => 'USA Nav' | |
) ); | |
} elseif( geot_target( 'AR' ) ) { | |
wp_nav_menu( array( | |
'menu' => 'Argentina Nav' | |
) ); | |
} else { | |
// show default | |
wp_nav_menu( array( | |
'menu' => 'Default Nav' | |
) ); | |
} | |
} else { | |
// if plugin is not active show a default menu | |
wp_nav_menu( array( | |
'menu' => 'Default Nav' | |
) ); | |
} |