WordPress UserAgent代码版
发布时间:2025-10-22 16:49:44 本站作者 【 字体:大 中 小 】 浏览:69 次
非插件实现显示评论者所用的浏览器和操作系统类型。该精简版原作者为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);?>
将该函数插入到评论列表的相应位置显示即可。
热门推荐
最新发布
PS4Slim变7吋掌机!大神改造视觉PG街头涂鸦实机展示
随着掌机市场持续升温,一位改装大神成功将PS4 Slim改造成7吋OLED掌机,并公开结合PG街头涂鸦视觉风格的实机展示画面,迅速吸引PG电子玩家关注。该设备可稳定运行主机内容,续航约3小时,并对主板结构与散热系统进行深度重构,搭配独立风扇与实时温控设计,确保试玩过程顺畅稳定。7 吋OLED屏幕带来更出色的画面表现,提升PG...
PPT文字怎么转换成SmartArt图形?PPT文字转为SmartArt图形的方法
PPT文字怎么转换成SmartArt图形?SmartArt是一项图形功能,可以快速、轻松、有效地传达信息,如果你的PPT中如果有多段文字并列显示,那么可以将其转换为SmartArt图形,下面我们来看看这篇PPT文字转为SmartArt图形的方法吧。具体操作如下:打开PPT,建立空白演示文稿。在幻灯片中插入文本框,文本框内有多个...
PPT如何制作彩色色块拼接字?PPT彩色色块拼接字制作教程
PPT如何制作彩色色块拼接字?PPT是款十分专业且实用的文档演示软件,近期有用户想要对PPT内的文字进行重新设计,将其制作成彩色色块拼接字,那么应该如何操作呢?不清楚具体步骤的朋友们可以来看看下面这篇教程。打开PPT,建立空白文稿。点击【插入】—【文本框】,单击鼠标,输入一个文字。选择一种字体,...
PPT如何绘制三线表格?PPT制作三线表的方法
PPT如何绘制三线表格?在我们日常PPT文档的编辑中,我们会添加各种表格来丰富文档内容,近期有用户想要在PPT中制作三线表,那么应该如何操作呢?不清楚具体步骤的朋友们,可以跟着下面这篇教程来操作。打开PPT,建立空白幻灯片。点击【插入】—【表格】,得到默认样式的表格。在表格中录入文字和数据。...
大家都在看
PS4Slim变7吋掌机!大神改造视觉PG街头涂鸦实机展示

PPT文字怎么转换成SmartArt图形?PPT文字转为SmartArt图形的方法

PPT如何制作彩色色块拼接字?PPT彩色色块拼接字制作教程

PPT如何绘制三线表格?PPT制作三线表的方法

PPT怎么让图片边缘变圆角?PPT图片边角变成圆形的方法

PPT幻灯片母版怎么重命名?PPT更改幻灯片母版名称的方法

PPT怎么设置成16:9?怎么调整PPT页面比例为16:9?

点击上方的【设计】选项。

点击上方的【设计】选项。

PPT里smartart怎么设置超链接?PPT给SmartArt图形设置超链接的方法

点击进入需要转化为SmartArt图形的幻灯片。

如图所示,将其转化为SmartArt图形。

将光标至于图形的文字当中。

按下键盘上的exc键,这样一来图形就被选中了。

点击进入需要转化为SmartArt图形的幻灯片。

如图所示,将其转化为SmartArt图形。

将光标至于图形的文字当中。

按下键盘上的exc键,这样一来图形就被选中了。

怎么更改PPT母版的标题?PPT修改母板标题样式的方法

PPT怎么绘制柠檬片?PPT画柠檬片的方法

