wordpress評論不具體記錄評論者的地理位置與瀏覽器類型,這個功能我們需要進入二次開發或使用插件,下面我介紹的不使用插件,是直接使用源碼操作,具體例子如下.
顯示評論者地理位置
將以下函數放到你的functions.php 中,實現的原理是利用新浪和淘寶的IP查詢接口,返回IP所屬城市,小修了下代碼,去掉了掛載點,直接在顯示評論時調用函數,代碼如下:
- /**
- * 使用api獲取<a title="查看與城市有關的文章" 城市名
- * @param string $ip
- * @return string|mixed
- */
- 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;
- }
- /**
- * 獲取當前用戶ip
- * @return string
- */
- 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 );
- // 如果api可以正常獲取到城市信息,則插入數據庫
- if ( $city ) {
- update_comment_meta( $comment->comment_ID, 'city_name', $city );
- } else {
- // 如果因為異常獲取不到api的信息,返回自定義字符串,留著下次讀取評論時再重新獲取
- $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等簡單的識別.
新聞熱點
疑難解答
圖片精選