我已安装此插件:https://tah.wordpress.org/plugins/geoip-detect/
当我测试"查找"插件似乎工作正常。在插件中,它返回我的地理信息。 但是,当我尝试在我的一个wordpress页面中实现代码时,它无法正常工作。
$ip = $_SERVER['REMOTE_ADDR'];
$userInfo = geoip_detect2_get_info_from_current_ip($ip);
echo $userInfo->country->name;
我在本地的woocommerce页面中调用该功能,其中显示了单个产品。 但是,这个功能一无所获。 我是否还需要包含更多内容来调用函数geoip_detect2_get_info_from_current_ip()?
我也尝试过:
<?php echo do_shortcode("[geoip_detect2 property='country']"); ?>
它也没有返回任何东西。 我在godaddy的代码编辑工具中进行编辑,所以我可能会错过错误。
答案 0 :(得分:2)
我已尝试在我的wordpress网站http://iradiofind.com/stations/中通过IP实施GEO。我正在使用http://www.geoplugin.net来获取国家/地区信息。获取IP地址我正在使用此功能
function get_client_ip() {
$ipaddress = '';
if (getenv('HTTP_CLIENT_IP'))
$ipaddress = getenv('HTTP_CLIENT_IP');
else if(getenv('HTTP_X_FORWARDED_FOR'))
$ipaddress = getenv('HTTP_X_FORWARDED_FOR');
else if(getenv('HTTP_X_FORWARDED'))
$ipaddress = getenv('HTTP_X_FORWARDED');
else if(getenv('HTTP_FORWARDED_FOR'))
$ipaddress = getenv('HTTP_FORWARDED_FOR');
else if(getenv('HTTP_FORWARDED'))
$ipaddress = getenv('HTTP_FORWARDED');
else if(getenv('REMOTE_ADDR'))
$ipaddress = getenv('REMOTE_ADDR');
else
$ipaddress = 'UNKNOWN';
return $ipaddress;
}
function ip_details($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
这是在我的页面模板中。
<?php
$myipd = get_client_ip();
$url = 'http://www.geoplugin.net/json.gp?ip='.$myipd;
$details = ip_details($url);
$v = json_decode($details);
$mycountry = $v->geoplugin_countryName;
?>
答案 1 :(得分:1)
我最近使用相同的插件进行自定义解决方案 - 我注意到该插件内置了IP检测功能 - geoip_detect2_get_client_ip() - 尝试使用它?
从您的代码编辑:
$ip = geoip_detect2_get_client_ip();
$userInfo = geoip_detect2_get_info_from_current_ip($ip);
echo $userInfo->country->name;
此功能也内置了缓存功能,在我的测试中看起来非常快。
答案 2 :(得分:0)
也许 geoip-detect 函数超出了你的范围(这会非常奇怪)。将其添加到 functions.php :
require_once ABSPATH . '/wp-content/plugins/geoip-detect/geoip-detect.php';