首先需要安装两个插件:1.GeoIP Detection (Yellow Tree)用于判断得到IP所在地 2.Polylang (Frédéric Demarle)自动切换语言。
在Polylang的设置中打开浏览器自动检测。
如果是简单的使用判断大陆\非大陆,去http://dev.maxmind.com/geoip/geoip2/geolite2/下载一个country版的IP库包。
在/wp-content/plugins/polylang/frontend/choose_lang.php中,首行添加以下代码。
1include "../../geoip-detect/api.php";上面文件中的函数get_preferred_browser_language中的首行添加以下代码:
123$viewer_location_info = geoip_detect2_get_info_from_ip($_SERVER['REMOTE_ADDR'], NULL);$viewer_location = $viewer_location_info->country->isoCode;if($viewer_location == 'CN') {return 'cn';}