美好的明天
分享改变,记录生活
美好的明天
当前位置: 首页 > WordPress > 正文

主题评论归属地测试

前言

阿里云 - 《全球IP归属地查询IP地址查询IPv6地址查询IP地理位置库查询IP离线库查询私有化部署离线查询》,同时支持 IPv4 和 IPv6 地址归属地查询,价钱还可以(30块钱查询100万次)。

目前本博客评论区归属地显示基于此 API 实现。要是有更便宜好用的可以偷偷告诉我哦 😄

代码片段

请求示例

以下是使用 PHP 语言进行请求的示例代码,通过 cURL 发起请求,实现对阿里云 IP 归属地查询 API 的调用:


<?php
    $host = "https://c2ba.api.huachen.cn";
    $path = "/ip";
    $method = "GET";
    $appcode = "你自己的AppCode";
    $headers = array();
    array_push($headers, "Authorization:APPCODE " . $appcode);
    $querys = "ip=58.242.2.2";
    $bodys = "";
    $url = $host . $path . "?" . $querys;

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($curl, CURLOPT_FAILONERROR, false);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_HEADER, true);
    if (1 == strpos("$".$host, "https://"))
    {
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    }
    var_dump(curl_exec($curl));
?>

调用结果

调用上述 API 示例代码后,得到的返回结果如下,包含了 IP 地址的详细归属地信息,如运营商、地区、城市等:


{
  "ret": 200,
  "msg": "success",
  "data": {
    "ip": "58.242.2.2",
    "long_ip": "988938754",
    "isp": "中国联通",
    "area": "华东",
    "region_id": "340000",
    "region": "安徽",
    "city_id": "340100",
    "city": "合肥",
    "district": "蜀山区",
    "district_id": "340104",
    "country_id": "CN",
    "country": "中国",
    "lat": "31.851158",
    "lng": "117.260536"
  },
  "log_id": "3f4a10b492a94526929d121065a6ceff"
}

WordPress 后台评论批量更新归属地

在 WordPress 后台实现评论批量更新归属地的功能,包含了自定义评论列表列(显示归属地信息)、添加批量更新动作以及处理更新操作和显示更新通知等功能模块,以下是相关的 PHP 代码:


/*-----------------------------------------------------------------------------------*/
// 后台评论列表自定义列(归属地显示)
/*-----------------------------------------------------------------------------------*/
function custom_comment_column($columns) {
    $columns['location_info'] = '归属地';
    return $columns;
}
add_filter('manage_edit-comments_columns', 'custom_comment_column');

function custom_comment_column_data($column_name, $comment_id) {
    if ($column_name === 'location_info') {
        $country = get_comment_meta($comment_id, 'country', true) ?: '';
        $region = get_comment_meta($comment_id, 'region', true) ?: '';
        $city = get_comment_meta($comment_id, 'city', true) ?: '';
        
        echo esc_html("$country $region $city $isp");
    }
}
add_action('manage_comments_custom_column', 'custom_comment_column_data', 10, 2);

/*-----------------------------------------------------------------------------------*/
// 后台批量更新归属地功能
/*-----------------------------------------------------------------------------------*/
function add_bulk_ip_update_action($actions) {
    $actions['update_ip_location'] = '更新归属地';
    return $actions;
}
add_filter('bulk_actions-edit-comments', 'add_bulk_ip_update_action');

function process_bulk_ip_update($redirect_to, $doaction, $comment_ids) {
    if ($doaction === 'update_ip_location') {
        $updated_count = 0;
        foreach ($comment_ids as $comment_id) {
            $comment = get_comment($comment_id);
            if ($comment && $comment->comment_author_IP) {
                $result = get_ip_province($comment->comment_author_IP, $comment_id);
                if ($result) $updated_count++;
            }
        }
        $redirect_to = add_query_arg(array(
            'ip_updated' => $updated_count,
            'updated' => true
        ), $redirect_to);
    }
    return $redirect_to;
}
add_filter('handle_bulk_actions-edit-comments', 'process_bulk_ip_update', 10, 3);

// 后台通知提示
function display_ip_update_notice() {
    if (isset($_GET['ip_updated'])) {
        $count = intval($_GET['ip_updated']);
        echo '<div class="updated notice is-dismissible"><p>' . sprintf(__('成功更新 %d 条评论的归属地信息'), $count) . '</p></div>';
    }
}
add_action('admin_notices', 'display_ip_update_notice');

思路

在显示归属地信息时,根据获取到的数据进行逻辑判断:

  • country 数据是 中国 时,则显示 region 对应的省份信息。
  • country 数据是 中国 以外的国家时,则显示 country 对应的国家名称。
  • 如果在评论时开启了代理,导致没有获取到 IP 地址,那么就不显示归属地信息。

本文评论区可查看上述功能的实际效果。

温馨提示

本页面最后更新于:2025-05-07,距今已 5 天,若有链接失效或教程无效,欢迎留言反馈。

THE END

猜你喜欢

目前有 27 条评论

  1. 老何
    25楼
    老何

    显示到省一级免费的就可以了呀

    2025-04-24 21:35 来自:安徽 回复
  2. Vei
    24楼
    Vei

    我也来试试

    2025-04-24 15:44 来自:四川 回复
  3. 棕耳兔
    23楼
    棕耳兔

    评论归属地测试23

    2025-04-22 00:55 回复
  4. 棕耳兔
    22楼
    棕耳兔

    评论归属地测试22

    2025-04-22 00:03 来自:南非 回复
  5. 棕耳兔
    21楼
    棕耳兔

    评论归属地测试21

    2025-04-22 00:03 来自:阿联酋 回复
    • CF673X
      CF673X

      @棕耳兔无法登录了吗

      2025-04-23 21:05 来自:云南 回复
      • 棕耳兔
        棕耳兔

        @CF673X可以登录,还有人评论呢,

        2025-04-24 12:27 来自:安徽

发表评论

电子邮件地址不会被公开。 必填项已用 * 标注

gravatar

OK aixin aoman baoquan bizui cahan caidao ciya dabing doge fadai ganga guzhang haixiu hanxiao huaixiao jie jingkong keai koubi ku leiben lenghan liuhan nanguo penxue piezui qiang qinqin quantou se shengli shuai tiaopi touxiao tuosai weiqu woshou wozuimei wunai xiaojiujie xiaoku xieyanxiao xigua yinxian yiwen youling yun