我想知道使用PHP的访客国家并将其显示在wordpress页面中.但是当我在wordpress页面或Post中添加PHP代码时,它会给我错误.
我们如何在wordpress页面和帖子中添加PHP代码.
<?PHP
try{
function visitor_country()
{
$client = @$_SERVER['HTTP_CLIENT_IP'];
$forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];
$remote = $_SERVER['REMOTE_ADDR'];
$result = "UnkNown";
if(filter_var($client, FILTER_VALIDATE_IP))
{
$ip = $client;
}
elseif(filter_var($forward, FILTER_VALIDATE_IP))
{
$ip = $forward;
}
else
{
$ip = $remote;
}
$ip_data = @json_decode(file_get_contents("http://www.geoplugin.net/json.gp?ip=".$ip));
if($ip_data && $ip_data->geoplugin_countryName != null)
{
$result = array('ip'=>$ip,
'continentCode'=>$ip_data->geoplugin_continentCode,
'countryCode'=>$ip_data->geoplugin_countryCode,
'countryName'=>$ip_data->geoplugin_countryName,
);
}
return $result;
}
$visitor_details= visitor_country(); // Output Country name [Ex: United States]
$country=$visitor_details['countryName'];
解决方法:
默认情况下,wordpress不会在帖子/页面内容中执行PHP,除非它有短代码.
最快捷,最简单的方法是使用一个插件,允许您运行嵌入在帖子内容中的PHP.
>使它成为一个短代码(把它放在functions.PHP中并让它回显国名)这很容易 – 见这里:Shortcode API at WP Codex
>将其放入模板文件中 – 根据您的默认页面模板为该页面创建自定义模板,并将PHP添加到模板文件中,而不是发布内容:Custom Page Templates
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。