WordPress 评论显示IP归属地 [复制链接]

帖子链接已复制到剪贴板
十年_obaby (UID: 3117) 2022-8-21

1519 4

最近流行显示ip归属地了,不管是抖音、知乎、微博还是各种小网站都在弄这个。这几天又开始折腾博客,把邮件通知加上了,于是今天就想着把显示归属地再加上。随便搜了一下,各种文章还真不少。

 

 

不过内容感觉像各种搬运的,实际操作起来也没不是很准确,当然还是有一定的参考价值的。后来找了个整合的代码,参考这里:http://zhangge.net/4960.html。里面提到了一些内容,通过修改 functions.php  和   comments.php来实现效果。很不幸的是我目前使用的主体,在comments文件中很难定位评论生成的位置,我又不想去找各种回调在修改。于是就想着用偷懒的办法。

通过安装WP-UserAgent插件,可以正常的显示浏览器信息,操作系统信息,现在唯一欠缺的就是归属地信息了。

通过下面的步骤就可以实现归属地显示了:

1.下载依赖文件:https://cloud.189.cn/t/Jn6nmu6Zb6vy (访问码:94my)

插件下载完成之后,上传到WP-UserAgent目录下(注意文件路径):

2. 修改修改vim wp-useragent.php 添加以下代码:

在文件头添加:

# 添加到文件头的其他include下面即可
include("show-useragent/show-useragent.php");
 
include("show-useragent/ip2c-text.php");

在显示函数内添加:

// 以下内容添加到 function wpua_display_useragent($wpua_wrapper_div = false)函数
$ip = get_comment_author_IP();
// echo $ip;
// echo CID_get_flag_without_template($ip);
//echo convertip($ip);
$wpua_useragent .= CID_get_flag_without_template($ip,true,false);
$wpua_useragent .= convertip($ip);
 
 
//添加到以下行的上面
// Does the user want to display the full useragent string?
if ($wpua_show_full_ua === 'true')
{
// Attach the full ua string to the output.
$br = (strlen($wpua_useragent) > 0) ? (($wpua_doctype === 'html') ? '<br>' : '<br />') : '';
$wpua_useragent .= "$br<small>".wpua_str_escape($comment->comment_agent)."</small>";
}
 
if ($wpua_wrapper_div === true)
{
// Wrap WP-UserAgent output in div
$wpua_useragent = "<div class='wp-useragent'>$wpua_useragent</div>";
}
 
// The following conditional will hopefully prevent a problem where
// the echo statement will interrupt redirects from the comment page.
if (empty($_POST['comment_post_ID']) || is_admin()) echo $wpua_useragent;

3. 到这里基本就能显示归属地了,但是国旗显示可能会存在问题,这个是由于插件的资源搜索路径导致的,最简单的把那就是把show-useragent直接上传到当前激活主题的文件夹下一份。现在就可以正常的显示归属地信息:

 

 

4. 浏览器信息,操作系统信息在WP-UserAgent插件的设置功能中修改即可,后台也可以显示归属地等相关信息。

 

 

PS:建议修改之前先备份插件,修改过程中如果出错可以直接恢复。

其他的一些相关资源:

原文链接:http://h4ck.org.cn/2022/08/wordpress-%E8%AF%84%E8%AE%BA%E6%98%BE%E7%A4%BAip%E5%BD%92%E5%B1%9E%E5%9C%B0/

这家伙太懒了,什么也没留下。
已有评论 (4)
提示:您必须 登录 才能查看此内容。
创建新帖
广告推广点击空位自助购买