wordpress显示评论者地理位置与浏览器类型
wordpress评论不具体记录评论者的地理位置与浏览器类型,这个功能我们需要进入二次开发或使用插件,下面我介绍的不使用插件,是直接使用源码操作,具体例子如下.
显示评论者地理位置
将以下函数放到你的functions.php 中,实现的原理是利用新浪和淘宝的IP查询接口,返回IP所属城市,小修了下代码,去掉了挂载点,直接在显示评论时调用函数,代码如下:
-
-
-
-
-
- function wpgo_get_city($ip = null) {
- $ip = $ip == null ? wpgo_get_ip() : $ip;
- $ipApi = array (
- 'http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip=',
- 'http://ip.taobao.com/service/getIpInfo.php?ip='
- );
-
- foreach ( $ipApi as $k=> $api ) {
- $res = wp_remote_get ( $api . $ip );
- if ( is_object ( $res ) && $k == 0 ) {
- continue;
- }
- if (! emptyempty ( $res ['body'] )) {
- $return = json_decode ( $res ['body'], true );
- if (! emptyempty ( $return ['city'] )) {
- return $return ['city'];
- } elseif( $return ['province'] == '香港' || $return ['province'] == '澳门') {
- return $return ['province'];
- } else {
- return $return ['country'];
- }
- }
- }
- return false;
- }
-
-
-
-
-
- function wpgo_get_ip() {
- if ( getenv ( "HTTP_CLIENT_IP" ) && strcasecmp ( getenv ( "HTTP_CLIENT_IP" ), "unknown" ) ) {
- $ip = getenv ( "HTTP_CLIENT_IP" );
- } elseif (getenv ( "HTTP_X_FORWARDED_FOR" ) && strcasecmp ( getenv ( "HTTP_X_FORWARDED_FOR" ), "unknown" )) {
- $ip = getenv ( "HTTP_X_FORWARDED_FOR" );
- } elseif (getenv ( "REMOTE_ADDR" ) && strcasecmp ( getenv ( "REMOTE_ADDR" ), "unknown" )) {
- $ip = getenv ( "REMOTE_ADDR" );
- } elseif (isset ( $_SERVER ['REMOTE_ADDR'] ) && $_SERVER ['REMOTE_ADDR'] && strcasecmp ( $_SERVER ['REMOTE_ADDR'], "unknown" )) {
- $ip = $_SERVER ['REMOTE_ADDR'];
- }
- if ($_SERVER ['REMOTE_ADDR'] == '127.0.0.1' && $ip == 'unknown') {
- $ip = 'localhost';
- }
- return $ip;
- }
最重要的一段:最后在你的模板输出评论之前,获取城市字段,下面是引用我模板里的代码,你只能参考判断方式,具体怎么修改得根据你的模板来修改,代码如下:
-
- if ( $is_admin ) {
- $city = '来自管理员的回复';
- } else {
-
- $city = get_comment_meta( $comment->comment_ID, 'city_name', true );
- if( !$city ) {
- $city = wpgo_get_city ( $comment->comment_author_IP );
-
- if ( $city ) {
- update_comment_meta( $comment->comment_ID, 'city_name', $city );
- } else {
-
- $city = '火星';
- }
- }
- $city = "来自{$city}的网友";
- }
显示评论者浏览器类型,将下面代码放入function.php中,代码如下:
- function getbrowser($Agent)
- {
- if ($Agent == "")
- $Agent = $_SERVER['HTTP_USER_AGENT'];
- $browser = '';
- $browserver = '';
- if(ereg('Mozilla', $Agent) && ereg('Chrome', $Agent))
- {
- $temp = explode('(', $Agent);
- $Part = $temp[2];
- $temp = explode('/', $Part);
- $browserver = $temp[1];
- $temp = explode(' ', $browserver);
- $browserver = $temp[0];
- $browser = 'Chrome';
- }
- if(ereg('Mozilla', $Agent) && ereg('Firefox', $Agent))
- {
- $temp = explode('(', $Agent);
- $Part = $temp[1];
- $temp = explode('/', $Part);
- $browserver = $temp[2];
- $temp = explode(' ', $browserver);
- $browserver = $temp[0];
- $browser = 'Firefox';
- }
- if(ereg('Mozilla', $Agent) && ereg('Opera', $Agent))
- {
- $temp = explode('(', $Agent);
- $Part = $temp[1];
- $temp = explode(')', $Part);
- $browserver = $temp[1];
- $temp = explode(' ', $browserver);
- $browserver = $temp[2];
- $browser = 'Opera';
- }
- if(ereg('Mozilla', $Agent) && ereg('UCBrowser', $Agent))
- {
- $temp = strrchr($Agent,'/');
- $browserver = substr($temp,1);
- $browser = 'UC';
- }
- if(ereg('Mozilla', $Agent) && ereg('MSIE', $Agent))
- {
- $temp = explode('(', $Agent);
- $Part = $temp[1];
- $temp = explode(';', $Part);
- $Part = $temp[1];
- $temp = explode(' ', $Part);
- $browserver = $temp[2];
- $browser = 'Internet Explorer';
- }
-
- if($browser != '')
- {
- $browseinfo = $browser.' '.$browserver;
- }
- else
- {
- $browseinfo = $Agent;
- }
-
- return $browseinfo;
- }
上面的getbrowser()函数返回的是浏览器名字+浏览器版本,在相关位置调用,让其显示出来即可,最后打开wordpress下的wp-includes/comment-template,查找function get_comment_author_link函数,在最后一个return之前加入调用函数,以及显示对应小图标功能,代码如下:
- if($comment)
- $ua = $comment->comment_agent;
- else
- $ua = "";
- $tmp = getbrowser($ua);
- if($tmp != "") {
- $br = explode(' ',$tmp);
- if(stristr($br[0],'chrome'))
- $brimg = "/chrome.png";
- elseif(stristr($br[0],'firefox'))
- $brimg = "/firefox.png";
- elseif(stristr($br[0],'opera'))
- $brimg = "/opera.png";
- elseif(stristr($br[0],'internet'))
- $brimg = "/ie.png";
- elseif(stristr($br[0],'Safari'))
- $brimg = "/Safari.png";
- elseif(stristr($br[0],'UC'))
- $brimg = "/ucweb.png";
- else
- $brimg = "/anonymouse.png";
- $return .= " <img src='".$brimg."' title='".getbrowser($ua)."' />";
- }
到这里,大功告成,剩下的有时间的话,再把其他浏览器补全了,目前只支持chrome,ie,firefox,opera等简单的识别.