WordPress UserAgent代码版
发布时间:2025-10-22 16:49:44 本站作者 【 字体:大 中 小 】 浏览:144 次
非插件实现显示评论者所用的浏览器和操作系统类型。该精简版原作者为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);?>
将该函数插入到评论列表的相应位置显示即可。
热门推荐
最新发布
白菜豆腐的家常做法
用料主料 白菜800克 豆腐400克调料 食盐1.5茶匙 鸡精1/2茶 匙姜1片 蒜3瓣 植物油3汤匙 细香葱1根 水少许白菜豆腐的做法1.白菜切段,豆腐切块。2.豆腐和白菜各下水焯一下,捞出沥干;锅中倒植物油烧热,放入姜蒜葱,炒香。3.放入豆腐略煎一下,加水(或高汤)小炖一会儿;放入白菜翻炒几下,最后调...
菠菜炒鸡胸肉的做法
材料菠菜适量,鸡胸肉适量,盐适量,鸡精适量,五香粉适量,生抽适量,老抽适量,淀粉适量,料酒适量,姜适量,蒜适量,花椒适量。具体方法1.准备适量鸡胸肉,洗净后切成条,放入适量的姜丝,再放入适量盐,鸡精,五香粉,淀粉,搅拌均匀,腌制15分钟。2.炒锅中放入适量油,油热后放入适量蒜片和花椒爆...
家常香菇菜心的做法
用料主料香菇 250克油菜苔 250克调料色拉油 适量食盐 适量蚝油 适量淀粉 适量白糖 少许香菇菜心的做法1. 菜心清洗干净。2. 水中加少许盐和油,烧开焯烫菜心。这样焯过的蔬菜能保持很好的色泽,其他蔬菜也适用。3. 菜心捞出过凉。4. 挑选大小基本一致,表面光洁...
家常芹菜炒土豆片的做法
用料主料芹菜 200g马铃薯丁(脱水) 2个调料食盐 适量味精 适量葱 适量花椒 适量生抽 适量调和油 适量柿子椒 适量芹菜炒土豆片的做法1.芹菜洗净,切小段。2.土豆切片,用冷水浸泡,备用。3.开水焯一下,放入冷水中。4.锅中放油,加葱、花椒炒香放入土豆...






