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

首頁 > CMS > Wordpress > 正文

wordpress顯示評論者地理位置與瀏覽器類型

2024-09-07 00:51:14
字體:
供稿:網(wǎng)友

wordpress評論不具體記錄評論者的地理位置與瀏覽器類型,這個功能我們需要進(jìn)入二次開發(fā)或使用插件,下面我介紹的不使用插件,是直接使用源碼操作,具體例子如下.

顯示評論者地理位置

將以下函數(shù)放到你的functions.php 中,實現(xiàn)的原理是利用新浪和淘寶的IP查詢接口,返回IP所屬城市,小修了下代碼,去掉了掛載點,直接在顯示評論時調(diào)用函數(shù),代碼如下:

  1. /** 
  2.  * 使用api獲取<a title="查看與城市有關(guān)的文章" 城市名 
  3.  * @param string $ip 
  4.  * @return string|mixed 
  5.  */ 
  6. function wpgo_get_city($ip = null) { 
  7.     $ip = $ip == null ? wpgo_get_ip() : $ip
  8.     $ipApi = array ( 
  9.             'http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip='
  10.             'http://ip.taobao.com/service/getIpInfo.php?ip=' 
  11.     ); 
  12.  
  13.     foreach ( $ipApi as $k=> $api ) { 
  14.         $res = wp_remote_get ( $api . $ip ); 
  15.         if ( is_object ( $res ) && $k == 0 ) { 
  16.             continue
  17.         } 
  18.         if (! emptyempty ( $res ['body'] )) { 
  19.             $return = json_decode ( $res ['body'], true ); 
  20.             if (! emptyempty ( $return ['city'] )) { 
  21.                 return $return ['city']; 
  22.             } elseif$return ['province'] == '香港' || $return ['province'] == '澳門') { 
  23.                 return $return ['province']; 
  24.             } else { 
  25.                 return $return ['country']; 
  26.             } 
  27.         } 
  28.     } 
  29.     return false; 
  30.  
  31. /** 
  32.  * 獲取當(dāng)前用戶ip 
  33.  * @return string 
  34.  */ 
  35. function wpgo_get_ip() { 
  36.     if ( getenv ( "HTTP_CLIENT_IP" ) && strcasecmp ( getenv ( "HTTP_CLIENT_IP" ), "unknown" ) ) { 
  37.         $ip = getenv ( "HTTP_CLIENT_IP" ); 
  38.     } elseif (getenv ( "HTTP_X_FORWARDED_FOR" ) && strcasecmp ( getenv ( "HTTP_X_FORWARDED_FOR" ), "unknown" )) { 
  39.         $ip = getenv ( "HTTP_X_FORWARDED_FOR" ); 
  40.     } elseif (getenv ( "REMOTE_ADDR" ) && strcasecmp ( getenv ( "REMOTE_ADDR" ), "unknown" )) { 
  41.         $ip = getenv ( "REMOTE_ADDR" ); 
  42.     } elseif (isset ( $_SERVER ['REMOTE_ADDR'] ) && $_SERVER ['REMOTE_ADDR'] && strcasecmp ( $_SERVER ['REMOTE_ADDR'], "unknown" )) { 
  43.         $ip = $_SERVER ['REMOTE_ADDR']; 
  44.     } 
  45.     if ($_SERVER ['REMOTE_ADDR'] == '127.0.0.1' && $ip == 'unknown') { 
  46.         $ip = 'localhost'
  47.     } 
  48.     return $ip

最重要的一段:最后在你的模板輸出評論之前,獲取城市字段,下面是引用我模板里的代碼,你只能參考判斷方式,具體怎么修改得根據(jù)你的模板來修改,代碼如下:

  1. // 如果是管理員回復(fù) 
  2. if ( $is_admin ) { 
  3.     $city = '來自管理員的回復(fù)'
  4. else { 
  5.     // 兼容以前還沒有城市字段的評論 
  6.     $city = get_comment_meta( $comment->comment_ID, 'city_name', true ); 
  7.     if( !$city ) { 
  8.         $city = wpgo_get_city ( $comment->comment_author_IP ); 
  9.         // 如果api可以正常獲取到城市信息,則插入數(shù)據(jù)庫 
  10.         if ( $city ) { 
  11.             update_comment_meta( $comment->comment_ID, 'city_name'$city ); 
  12.         } else { 
  13.         // 如果因為異常獲取不到api的信息,返回自定義字符串,留著下次讀取評論時再重新獲取 
  14.             $city = '火星'
  15.         } 
  16.     } 
  17.     $city = "來自{$city}的網(wǎng)友"

顯示評論者瀏覽器類型,將下面代碼放入function.php中,代碼如下:

  1. function getbrowser($Agent
  2.     { 
  3.         if ($Agent == ""
  4.   $Agent = $_SERVER['HTTP_USER_AGENT']; 
  5.         $browser = ''
  6.         $browserver = ''
  7.         if(ereg('Mozilla'$Agent) && ereg('Chrome'$Agent)) 
  8.         { 
  9.             $temp = explode('('$Agent); 
  10.             $Part = $temp[2]; 
  11.             $temp = explode('/'$Part); 
  12.             $browserver = $temp[1]; 
  13.             $temp = explode(' '$browserver); 
  14.             $browserver = $temp[0]; 
  15.             $browser = 'Chrome'
  16.         } 
  17.  if(ereg('Mozilla'$Agent) && ereg('Firefox'$Agent)) 
  18.         { 
  19.             $temp = explode('('$Agent); 
  20.             $Part = $temp[1]; 
  21.             $temp = explode('/'$Part); 
  22.             $browserver = $temp[2]; 
  23.             $temp = explode(' '$browserver); 
  24.             $browserver = $temp[0]; 
  25.            $browser = 'Firefox'
  26.         } 
  27.         if(ereg('Mozilla'$Agent) && ereg('Opera'$Agent))  
  28.         { 
  29.             $temp = explode('('$Agent); 
  30.             $Part = $temp[1]; 
  31.             $temp = explode(')'$Part); 
  32.             $browserver = $temp[1]; 
  33.             $temp = explode(' '$browserver); 
  34.             $browserver = $temp[2]; 
  35.             $browser = 'Opera'
  36.         } 
  37.  if(ereg('Mozilla'$Agent) && ereg('UCBrowser'$Agent))  
  38.         { 
  39.             $temp = strrchr($Agent,'/'); 
  40.             $browserver = substr($temp,1); 
  41.             $browser = 'UC'
  42.         } 
  43.         if(ereg('Mozilla'$Agent) && ereg('MSIE'$Agent)) 
  44.         { 
  45.             $temp = explode('('$Agent); 
  46.             $Part = $temp[1]; 
  47.             $temp = explode(';'$Part); 
  48.             $Part = $temp[1]; 
  49.             $temp = explode(' '$Part); 
  50.             $browserver = $temp[2]; 
  51.             $browser = 'Internet Explorer'
  52.         } 
  53.         //其余瀏覽器按需自己增加 
  54.         if($browser != ''
  55.         { 
  56.             $browseinfo = $browser.' '.$browserver
  57.         }  
  58.         else 
  59.         { 
  60.             $browseinfo = $Agent
  61.         } 
  62.    
  63.         return $browseinfo
  64.     } 

上面的getbrowser()函數(shù)返回的是瀏覽器名字+瀏覽器版本,在相關(guān)位置調(diào)用,讓其顯示出來即可,最后打開wordpress下的wp-includes/comment-template,查找function get_comment_author_link函數(shù),在最后一個return之前加入調(diào)用函數(shù),以及顯示對應(yīng)小圖標(biāo)功能,代碼如下:

  1. if($comment
  2.  $ua = $comment->comment_agent; 
  3. else 
  4.  $ua = ""
  5. $tmp = getbrowser($ua); 
  6. if($tmp != "") { 
  7.  $br = explode(' ',$tmp); 
  8.  if(stristr($br[0],'chrome')) 
  9.   $brimg = "/chrome.png"
  10.  elseif(stristr($br[0],'firefox')) 
  11.   $brimg = "/firefox.png"
  12.  elseif(stristr($br[0],'opera')) 
  13.   $brimg = "/opera.png"
  14.  elseif(stristr($br[0],'internet')) 
  15.   $brimg = "/ie.png"
  16.  elseif(stristr($br[0],'Safari')) 
  17.   $brimg = "/Safari.png"
  18.  elseif(stristr($br[0],'UC')) 
  19.   $brimg = "/ucweb.png"
  20.  else  
  21.   $brimg = "/anonymouse.png"
  22.  $return .= " <img src='".$brimg."' title='".getbrowser($ua)."' />"

到這里,大功告成,剩下的有時間的話,再把其他瀏覽器補(bǔ)全了,目前只支持chrome,ie,firefox,opera等簡單的識別.

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 女性| 峨边| 始兴县| 霍城县| 朝阳区| 淮南市| 明水县| 平果县| 郴州市| 兖州市| 怀化市| 岳池县| 景德镇市| 湘西| 金湖县| 惠来县| 永昌县| 秀山| 宜阳县| 会宁县| 大洼县| 利川市| 色达县| 汕头市| 孟州市| 长岭县| 甘南县| 保德县| 噶尔县| 英德市| 吉林市| 中卫市| 高唐县| 成安县| 保靖县| 衡南县| 丹巴县| 眉山市| 梅州市| 长汀县| 红桥区|