WordPress UserAgent代码版
发布时间:2025-10-22 16:49:44 本站作者 【 字体:大 中 小 】 浏览:114 次
非插件实现显示评论者所用的浏览器和操作系统类型。该精简版原作者为MUFENG
将UserAgent集成到你的主题中。下面是极简无图版介绍:
首先将下面的get_useragent()函数代码加入到functions.php模板中
<?php
function get_browsers($ua){
$title = 'unknow';
$icon = 'unknow';
if (preg_match('#MSIE ([a-zA-Z0-9.]+)#i', $ua, $matches)) {
$title = 'Internet Explorer '. $matches[1];
if ( strpos($matches[1], '7') !== false || strpos($matches[1], '8') !== false)
$icon = 'ie8';
elseif ( strpos($matches[1], '9') !== false)
$icon = 'ie9';
elseif ( strpos($matches[1], '10') !== false)
$icon = 'ie10';
else
$icon = 'ie';
}elseif (preg_match('#Firefox/([a-zA-Z0-9.]+)#i', $ua, $matches)){
$title = 'Firefox '. $matches[1];
$icon = 'firefox';
}elseif (preg_match('#CriOS/([a-zA-Z0-9.]+)#i', $ua, $matches)){
$title = 'Chrome for iOS '. $matches[1];
$icon = 'crios';
}elseif (preg_match('#Chrome/([a-zA-Z0-9.]+)#i', $ua, $matches)) {
$title = 'Google Chrome '. $matches[1];
$icon = 'chrome';
if (preg_match('#OPR/([a-zA-Z0-9.]+)#i', $ua, $matches)) {
$title = 'Opera '. $matches[1];
$icon = 'opera15';
if (preg_match('#opera mini#i', $ua)) $title = 'Opera Mini'. $matches[1];
}
}elseif (preg_match('#Safari/([a-zA-Z0-9.]+)#i', $ua, $matches)) {
$title = 'Safari '. $matches[1];
$icon = 'safari';
}elseif (preg_match('#Opera.(.*)Version[ /]([a-zA-Z0-9.]+)#i', $ua, $matches)) {
$title = 'Opera '. $matches[2];
$icon = 'opera';
if (preg_match('#opera mini#i', $ua)) $title = 'Opera Mini'. $matches[2];
}elseif (preg_match('#Maxthon( |/)([a-zA-Z0-9.]+)#i', $ua,$matches)) {
$title = 'Maxthon '. $matches[2];
$icon = 'maxthon';
}elseif (preg_match('#360([a-zA-Z0-9.]+)#i', $ua, $matches)) {
$title = '360 Browser '. $matches[1];
$icon = '360se';
}elseif (preg_match('#SE 2([a-zA-Z0-9.]+)#i', $ua, $matches)) {
$title = 'SouGou Browser 2'.$matches[1];
$icon = 'sogou';
}elseif (preg_match('#UCWEB([a-zA-Z0-9.]+)#i', $ua, $matches)) {
$title = 'UCWEB '. $matches[1];
$icon = 'ucweb';
}elseif(preg_match('#wp-(iPhone|Android)/([a-zA-Z0-9.]+)#i', $ua, $matches)){ // 1.2 增加 wordpress 客户端的判断
$title = 'wordpress '. $matches[2];
$icon = 'wordpress';
}
return array(
$title,
$icon
);
}
function get_os($ua){
$title = 'unknow';
$icon = 'unknow';
if (preg_match('/win/i', $ua)) {
if (preg_match('/Windows NT 6.1/i', $ua)) {
$title = "Windows 7";
$icon = "windows_win7";
}elseif (preg_match('/Windows NT 5.1/i', $ua)) {
$title = "Windows XP";
$icon = "windows";
}elseif (preg_match('/Windows NT 6.2/i', $ua)) {
$title = "Windows 8";
$icon = "windows_win8";
}elseif (preg_match('/Windows NT 6.3/i', $ua)) {
$title = "Windows 8.1";
$icon = "windows_win8";
}elseif (preg_match('/Windows NT 6.0/i', $ua)) {
$title = "Windows Vista";
$icon = "windows_vista";
}elseif (preg_match('/Windows NT 5.2/i', $ua)) {
if (preg_match('/Win64/i', $ua)) {
$title = "Windows XP 64 bit";
} else {
$title = "Windows Server 2003";
}
$icon = 'windows';
}elseif (preg_match('/Windows Phone/i', $ua)) {
$matches = explode(';',$ua);
$title = $matches[2];
$icon = "windows_phone";
}
}elseif (preg_match('#iPod.*.CPU.([a-zA-Z0-9.( _)]+)#i', $ua, $matches)) {
$title = "iPod ".$matches[1];
$icon = "iphone";
} elseif (preg_match('#iPhone OS ([a-zA-Z0-9.( _)]+)#i', $ua, $matches)) {// 1.2 修改成 iphone os 来判断
$title = "Iphone ".$matches[1];
$icon = "iphone";
} elseif (preg_match('#iPad.*.CPU.([a-zA-Z0-9.( _)]+)#i', $ua, $matches)) {
$title = "iPad ".$matches[1];
$icon = "ipad";
} elseif (preg_match('/Mac OS X.([0-9. _]+)/i', $ua, $matches)) {
if(count(explode(7,$matches[1]))>1) $matches[1] = 'Lion '.$matches[1];
elseif(count(explode(8,$matches[1]))>1) $matches[1] = 'MountAIn Lion '.$matches[1];
$title = "Mac OSX ".$matches[1];
$icon = "macos";
} elseif (preg_match('/Macintosh/i', $ua)) {
$title = "Mac OS";
$icon = "macos";
} elseif (preg_match('/CrOS/i', $ua)){
$title = "Google Chrome OS";
$icon = "chrome";
}elseif (preg_match('/Linux/i', $ua)) {
$title = 'Linux';
$icon = 'linux';
if (preg_match('/Android.([0-9. _]+)/i',$ua, $matches)) {
$title= $matches[0];
$icon = "android";
}elseif (preg_match('#Ubuntu#i', $ua)) {
$title = "Ubuntu Linux";
$icon = "ubuntu";
}elseif(preg_match('#Debian#i', $ua)) {
$title = "Debian GNU/Linux";
$icon = "debian";
}elseif (preg_match('#Fedora#i', $ua)) {
$title = "Fedora Linux";
$icon = "fedora";
}
}
return array(
$title,
$icon
);
}
function get_useragent($ua){
$url = get_bloginfo('template_directory') . '/images/browsers/';
$browser = get_browsers($ua);
$os = get_os($ua);
echo '<span class="useragent tra">|<span class="fa fa-globe"></span> '.$browser[0].'|<span class="fa fa-desktop"></span> '.$os[0].'</span>';
//echo '<img src="'.$url.$browser[1].'.png" title="'.$browser[0].'" style="border:0px;vertical-align:middle;" alt="'.$browser[0].'"><img src="'.$url.$os[1].'.png" title="'.$os[0].'" style="border:0px;vertical-align:middle;" alt="'.$os[0].'">';
}最后本人注释掉了原带图的代码,替换为无图模式,用到Font Awesome字体图标,可以自行添加自己想要的图标。
引用的代码为:
<?php get_useragent($comment->comment_agent);?>
将该函数插入到评论列表的相应位置显示即可。
热门推荐
最新发布
手撕蒜薹凉菜的做法(清爽口感,健康美味,让你欲罢不能)
在炎炎夏日,吃些清爽凉菜是再合适不过的了。而手撕蒜薹凉菜不仅味道鲜美,还有利于降血压、排毒养颜等功效,可以说是非常适合夏季食用的一道佳肴。一、准备食材首先我们需要准备的就是食材,要制作手撕蒜薹凉菜,需要准备的食材有:蒜薹、香菜、生姜、红辣椒、酱油、芝麻油、醋、盐和糖。二、清洗食材将蒜薹和香菜洗净,把...
香酥小海鱼的做法(以小海鱼为主角,轻松制作口感香酥的美食)
海鲜一直是人们餐桌上的美味佳肴,而以小海鱼为材料制作的香酥小海鱼更是备受大家喜爱。香酥小海鱼口感鲜美,外脆里嫩,营养丰富,是一道美味可口的海鲜佳肴。接下来我们就来一起学习如何制作香酥小海鱼,轻松享受美食的同时还能保证健康营养。一:准备材料准备材料是制作香酥小海鱼的第一步。需要准备的材料有小海鱼、面粉...
教你用白水煮出完美的鸡蛋(白水煮蛋,简单易学,健康美味,煮出口感最佳鸡蛋)
作为一种简单易学的食品制作方式,白水煮鸡蛋的口感和健康效益都深受人们喜爱。在本文中,我们将介绍如何用白水煮出口感最佳的鸡蛋。准备材料准备一些鸡蛋,清水和一个深度足够的小锅。选择鸡蛋选用质量良好、保存时间较短的新鲜鸡蛋,这样可以保证蛋黄和蛋白的质量更好。加水入锅在锅里倒入足够的水,水的高度要超过鸡蛋,...
白萝卜丝丸子汤,美味又养生(健康饮食之道,一碗汤养生又滋补)
在现代快节奏的生活中,我们常常忽略了对自己健康的关注和照顾。为了养成健康的饮食习惯,我们可以学习一些简单易操作的养生菜谱,如今天介绍的白萝卜丝丸子汤,它是一道兼具口感和营养价值的汤品。1.选料要点:选择好的白萝卜、猪肉和豆腐。2.制作白萝卜丝:将白萝卜去皮后切成细丝。3.炖制猪肉汤底:将猪肉切成小块,加入...
大家都在看
手撕蒜薹凉菜的做法(清爽口感,健康美味,让你欲罢不能)

香酥小海鱼的做法(以小海鱼为主角,轻松制作口感香酥的美食)

教你用白水煮出完美的鸡蛋(白水煮蛋,简单易学,健康美味,煮出口感最佳鸡蛋)

白萝卜丝丸子汤,美味又养生(健康饮食之道,一碗汤养生又滋补)

白萝卜丝丸子汤,美味又养生(健康饮食之道,一碗汤养生又滋补)

鲜美可口的鳕鱼豆腐汤(家庭厨房里的美味佳肴,健康营养更有口感)

手工制作猫耳朵零食(自制美味小零食,体验DIY乐趣)

用香芹干丝轻松制作美食(香芹干丝的多种用途及做法)

小笼包萝卜,美味的佳肴(用小笼包和萝卜打造独特口感)

自制猪肉松,美味健康不贵!(用自己的双手,做出美味的猪肉松,让生活更有情趣!)

