国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > CMS > Wordpress > 正文

WordPress顯示評論者IP、歸屬地及運營商信息解決方法

2024-09-07 00:52:12
字體:
來源:轉載
供稿:網友

本文我們分享一個 WordPress 利用在張API顯示 評論者IP、歸屬地及運營商信息的功能實現代碼,api有很多,本文以淘寶的舉例。

①、在線API

在線查詢IP信息的API有很多,比如淘寶,百度和新浪都有IP信息查詢的API接口,地址如下:

百度:http://apistore.baidu.com/microservice/iplookup?ip=114.114.114.114

淘寶:http://ip.taobao.com/service/getIpInfo.php?ip=114.114.114.114

新浪:http://int.dpool.sina.com.cn/iplookup/iplookup.php?ip=114.114.114.114

請求方式都是GET,返回都是json格式.

如何將這個東東應用到WordPress評論列表呢?很簡單,挑一個用得比較多的淘寶作為范例吧,喜歡其他的自行參考修改即可.

②、PHP代碼:

  1. /** 
  2. * WordPress 顯示評論者IP歸屬地PHP函數 
  3. **/ 
  4. function get_locate($ip) { 
  5.     if(emptyempty($ip)) $ip = get_comment_author_IP(); 
  6.     $ch = curl_init();   
  7.     $timeout = 5;   
  8.     <a href="/tags.php/curl_setopt/" target="_blank">curl_setopt</a> ($ch, CURLOPT_URL, 'http://ip.taobao.com/service/getIpInfo.php?ip='.$ip);   
  9.     curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);   
  10.     curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);   
  11.     $file_contents = curl_exec($ch);   
  12.     curl_close($ch);   
  13.     $result = json_decode($file_contents,true); 
  14.     if ($result['data']['country'] != '中國') { 
  15.         return $result['data']['country']; 
  16.     } else { 
  17.         return $result['data']['country'].'&nbsp;·&nbsp;'.$result['data']['region'].'&nbsp;·&nbsp;'.$result['data']['city'].'&nbsp;·&nbsp;'.$result['data']['isp']; 
  18.     } 
  19. /** 
  20. * WordPress 顯示評論者IP歸屬地PHP函數 
  21. **/ 
  22. function get_locate($ip) { 
  23.     if(emptyempty($ip)) $ip = get_comment_author_IP(); 
  24.     $ch = curl_init();   
  25.     $timeout = 5;   
  26.     curl_setopt ($ch, CURLOPT_URL, 'http://ip.taobao.com/service/getIpInfo.php?ip='.$ip);   
  27.     curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);   
  28.     curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);   
  29.     $file_contents = curl_exec($ch);   
  30.     curl_close($ch);   
  31.     $result = json_decode($file_contents,true); 
  32.     if ($result['data']['country'] != '中國') { 
  33.         return $result['data']['country']; 
  34.     } else { //Vevb.com 
  35.         return $result['data']['country'].'&nbsp;·&nbsp;'.$result['data']['region'].'&nbsp;·&nbsp;'.$result['data']['city'].'&nbsp;·&nbsp;'.$result['data']['isp']; 
  36.     } 

將上述代碼添加到WordPress主題函數模板文件 functions.php 中并保存,然后,在 WordPress 評論模板函數中合適的位置插入如下代碼即可:

<?php get_locate(get_comment_author_ip());?>

<?php get_locate(get_comment_author_ip());?>

Ps:大部分國內主題都會自定義一個評論模板函數,要找到這個函數很簡單,先打開主題下的評論模板文件comments.php文件,找到如下類似代碼:

<?php wp_list_comments( 'type=comment&callback=mytheme_comment' ); ?>

<?php wp_list_comments( 'type=comment&callback=mytheme_comment' ); ?>

如上代碼中的callback值則是主題自定義的評論模板回調函數,即 mytheme_comment.

接著,只要在主題下全局搜索 mytheme_comment 這個關鍵詞就能找到對應的函數啦,至于添加到哪就自己把握了,每個主題都不一樣,我就不贅述了.

這個在線查詢評論者歸屬地的效果忘記截圖了,我定義的是是 [中國 · 廣東 · 深圳 · 電信] 這種形式,喜歡的可以部署看看.

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 巴南区| 江口县| 洛川县| 涞水县| 陕西省| 武冈市| 海兴县| 潮安县| 蒙山县| 永年县| 陇西县| 濉溪县| 新竹县| 子长县| 合作市| 鹤峰县| 闻喜县| 兴和县| 河南省| 龙海市| 大方县| 木兰县| 内乡县| 鄢陵县| 赤水市| 祥云县| 卢氏县| 亳州市| 松阳县| 永年县| 叙永县| 闸北区| 城固县| 华安县| 祁东县| 宜黄县| 渝中区| 太仆寺旗| 兴和县| 奈曼旗| 西安市|