loader
Loading···

WordPress系统子比主题添加评论IP归属地

前言

这个有两个版本的教程,第一种比较常见,这次我两个都说一下!

1、文件版

由于本站用的第二种,此处就不放演示图了

1.1、上传文件

然后上传到程序子比主题,wp-content/themes/zibll目录里面,然后直接解压。

WordPress系统子比主题添加评论IP归属地

然后复制这个show-useragent文件目录,然后粘贴到网站根目录。

1.2、修改functions.php

子比主题可以将代码放进func.php,这样更新主题的时候不会丢失

include("show-useragent/show-useragent.php");   // 显示访客信息
include("show-useragent/ip2c-text.php");        // IP归属地和运营商查询功能

1.3、修改zib-comments-list.php

其他主题可能是comment.php,子比主题文件在wp-content/themes/zibll/inc/functions里面

$ip = convertip(get_comment_author_ip());
    return '<name class="flex ac flex1">' . $user_name . $badge .'<span class="badg badge badg-sm flex0 ml3">'.$ip.'</span>'.'</name>';

WordPress系统子比主题添加评论IP归属地

注意!下面这一步是评论UA显示,WordPress系统子比主题添加评论IP归属地不需要的可以忽略这样吧(真的丑的一批)

$diqu =  CID_get_comment_browser();
$diqu .= CID_print_comment_flag();
$time_html = '<span class="comt-author"' . ($is_mobile ? ' data-toggle="tooltip"' : '') . ' title="' . date('Y年m月d日 H:i:s', strtotime($comment->comment_date)) . '">' . zib_get_time_ago($comment->comment_date) . '</span>'.'<span class="comt-author" title="">'.$diqu.'</span>';

WordPress系统子比主题添加评论IP归属地

然后刷新即可!

2、插件版

这是一个开源的插件,教程耶差不多!

2.1、上传插件

WordPress系统子比主题添加评论IP归属地
启用插件

2.2、修改zib-comments-list.php

和文件版一样的位置

WordPress系统子比主题添加评论IP归属地
$ip_loction = easy_location_handle_comment($comment_text);
return '<name class="flex ac flex1">' . $user_name . $badge .'<span class="badg badge badg-sm flex0 ml3">'.$ip_loction.'</span>'.'</name>';

这就可以用了

2.3、扩展

WordPress系统子比主题添加评论IP归属地

插件的第十一行define(‘SHOW_POST_LOCATION’, false); 可以把false改成true,这样可以显示文章作者的IP归属地,演示本站就有,在文章的底部。

2.3.1、文章作者IP归属自定义位置

关于”扩展“显示的位置部分人可能不太满意,下面我教一下子比主题怎么自定义这个位置,其他主题大同小异。

/wp-content/themes/zibll/inc/functions/zib-user.php找到这个文件在第11591166

第1159行

理论上这是个空行,在这里加上:

$post_up_ip = easy_location_handle_post_content($content);
第1166行
$html .= $desc . ' ' . $post_up_ip;

’‘里面的空格不能去掉,要不然就和前面的字连起来了!

WordPress系统子比主题添加评论IP归属地
自定义位置效果图

如果你不需要文章底部再显示了,那就接着往下:

让我们回到/wp-content/plugins/easy-location/easy-location.php这个文件(没错又回来了

if (!function_exists('easy_location_handle_post_content')) :
function easy_location_handle_post_content($content)
{
global $post;
if (get_post_meta($post->ID, 'easy_location_ip', true) && SHOW_POST_LOCATION) {
$content .= '<div class="comment--location"><svg version="1.1" viewBox="0 0 368.666 368.666" width="14" height="14"><g><path d="M184.333,0C102.01,0,35.036,66.974,35.036,149.297c0,33.969,11.132,65.96,32.193,92.515
c27.27,34.383,106.572,116.021,109.934,119.479l7.169,7.375l7.17-7.374c3.364-3.46,82.69-85.116,109.964-119.51
c21.042-26.534,32.164-58.514,32.164-92.485C333.63,66.974,266.656,0,184.333,0z M285.795,229.355
c-21.956,27.687-80.92,89.278-101.462,110.581c-20.54-21.302-79.483-82.875-101.434-110.552
c-18.228-22.984-27.863-50.677-27.863-80.087C55.036,78.002,113.038,20,184.333,20c71.294,0,129.297,58.002,129.296,129.297
C313.629,178.709,304.004,206.393,285.795,229.355z" /><path d="M184.333,59.265c-48.73,0-88.374,39.644-88.374,88.374c0,48.73,39.645,88.374,88.374,88.374s88.374-39.645,88.374-88.374
S233.063,59.265,184.333,59.265z M184.333,216.013c-37.702,0-68.374-30.673-68.374-68.374c0-37.702,30.673-68.374,68.374-68.374
s68.373,30.673,68.374,68.374C252.707,185.341,222.035,216.013,184.333,216.013z" /></g></svg>来自' . get_user_city(get_post_meta($post->ID, 'easy_location_ip', true)) . '</div>';
}


return $content;
}
endif;

换成这个

if (!function_exists('easy_location_handle_post_content')) :
function easy_location_handle_post_content($content)
{
global $post;
if (get_post_meta($post->ID, 'easy_location_ip', true) && SHOW_POST_LOCATION) {
$content .= '<span class="up-ippppp"><svg t="1662544287744" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5853"><path d="M897.5 423.7c0-161.7-96.7-300.9-235.4-362.7-0.7-0.3-1.4-0.6-2.2-1-1.4-0.6-2.8-1.2-4.3-1.8-48-20.5-100.7-31.8-156.2-31.8-219.8 0-398.1 178.2-398.1 398.1 0 97 34.7 185.8 92.3 254.9 0.9 1.2 1.8 2.4 2.8 3.5l1.1 1.2c2 2.3 3.9 4.5 6 6.8l0.1-0.1 263.6 288.3c2.9 3.2 6.3 5.8 9.9 7.9 16.5 11.1 39.1 8.7 53-6.4L787 699.5l0.1 0.1c5.2-5.4 10.3-11 15.2-16.8l0.1-0.1c0.7-0.7 1.3-1.5 1.9-2.3 58.2-69.2 93.3-158.6 93.3-256.1v-0.3c-0.1-0.1-0.1-0.2-0.1-0.3z" fill="#707070" p-id="5854"></path><path d="M499.5 264.4c-88.6 0-160.5 71.9-160.5 160.5s71.9 160.5 160.5 160.5S660 513.5 660 424.9s-71.9-160.5-160.5-160.5z" fill="#FFFFFF" p-id="5855"></path></svg>来自' . get_user_city(get_post_meta($post->ID, 'easy_location_ip', true)) . '</span>';
}


return $content;
}
endif;

在你的主题自定义css添加就OK了

.theme-box.wp-posts-content span.up-ippppp {
display: none;
}

2.3.2、帖子内页自定义位置

根据2.3.1修改完之后,子比主题的帖子内页没办法显示归属地了

/wp-content/themes/zibll/inc/functions/bbs/inc/single.php在这个文件第128

$action .= $rewards_btn . $post_up_ip;

125行(这好像也是个空行)

$post_up_ip = easy_location_handle_post_content($content);
WordPress系统子比主题添加评论IP归属地

3、常见问题

3.1、Q:为什么我的网站评论IP好像不太准?

A:这个可能是因为使用了CDN等产品,需要在wp-config.php添加如下代码,即可解决!

//获取访客真实IP地址
if(isset($_SERVER['HTTP_X_FORWARDED_FOR']))
{
$list = explode(',',$_SERVER['HTTP_X_FORWARDED_FOR']);
$_SERVER['REMOTE_ADDR'] = $list[0];
};

直接添加在<?php下面

3.2、Q:我用的插件版怎么只显示省份,不显示城市呢?

A:在php文件里面找到第259行,有个[1],把1改成2即可,需要注意的是,改成2可能有部分评论不显示归属地了

可以改成这样:

if (!function_exists('get_user_city')) :
function get_user_city($ip)
{
$reader = new Reader(__DIR__ . '/ipipfree.ipdb');
try {
if($reader->find($ip)[2])
return $reader->find($ip) ? $reader->find($ip)[2] : false;
else
return $reader->find($ip) ? $reader->find($ip)[1] : false;
} catch (\Throwable $th) {
return false;
}
}
endif;

3.3、Q:插件版文章底部不显示了,只有“来自”两个字,怎么搞?

A:依然打开插件php文件,来到第318行,将一整行都换掉就欧克了,你要是找不到,直接搜索onlineip就行

update_post_meta($post_id, 'easy_location_ip', explode(',', $onlineip)[0]);

3.4、Q:我……

A:

WordPress系统子比主题添加评论IP归属地

给TA买糖
共{{data.count}}人
人已买糖
9 条回复 UP文章作者
  • 小黄脸
  • AC娘
  • 米游姬
  • 蔚蓝档案
  1. 拉米娅_Official
    拉米娅_Official给作者打赏了¥2
  2. 拉米娅_Official
    拉米娅_Official给作者打赏了¥2
  3. 拉米娅_Official
    拉米娅_Official给作者打赏了¥2
  4. 拉米娅_Official
    拉米娅_Official给作者打赏了¥2
  5. 拉米娅_Official
    拉米娅_Official给作者打赏了¥2
  6. 拉米娅_Official
    拉米娅_Official给作者打赏了¥2
  7. 拉米娅_Official
    拉米娅_Official给作者打赏了¥2
  8. 拉米娅_Official
    拉米娅_Official给作者打赏了¥2
  9. 拉米娅_Official
    拉米娅_Official给作者打赏了¥50