之前网站上分享过一篇 DUX主题评论添加VIP星级与博主认证 当然了,这篇文章的代码不单单适用于DUX主题,对于一些其他的主题来说也是同样使用的,之所以这么命名是因为当时文章写的比较匆忙,没有对其他主题进行测试,本着对每一位网站浏览者负责的态度便这么命名了,而我这次分享的评论框显示用户浏览器及操作系统本人已经测试过几款不同的WordPress主题了,表示显示起来并没有发现什么问题。

这次对主题的修改操作仅改动了functions.php及mo_comments_list.php(DUX主题,其他主题可自行选择标识显示位置)两个文件,大家在修改之前先备份下这两个文件以免误操作引起网站的崩溃。

functions.php文件修改

在主题的functions.php文件中添加如下函数:

//获取用户UA信息  
function user_agent($ua){
//开始解析操作系统  
$os = null;
if(preg_match('/Windows 95/i',$ua) || preg_match('/Win95/',$ua)){
    $os="Windows 95";}
elseif(preg_match('/Windows NT 5.0/i',$ua) || preg_match('/Windows 2000/i', $ua)){
    $os="Windows 2000";}
elseif(preg_match('/Win 9x 4.90/i',$ua) || preg_match('/Windows ME/i', $ua)){
    $os="Windows ME";}
elseif(preg_match('/Windows.98/i',$ua) || preg_match('/Win98/i', $ua)){
    $os = "Windows 98";}
elseif(preg_match('/Windows NT 6.0/i',$ua)){
    $os="Windows Vista";}
elseif(preg_match('/Windows NT 6.1/i',$ua)){
    $os="Windows 7";}
elseif(preg_match('/Windows NT 5.1/i',$ua)){
    $os = "Windows XP";}
elseif(preg_match('/Windows NT 5.2/i',$ua) && preg_match('/Win64/i',$ua)){
    $os="Windows XP 64 bit";}
elseif(preg_match('/Windows NT 5.2/i',$ua)){
    $os="Windows Server 2003";}
elseif(preg_match('/Mac_PowerPC/i',$ua)){
    $os="Mac OS";}
elseif(preg_match('/Windows Phone/i',$ua)){
    $os="Windows Phone7";}
elseif (preg_match('/Windows NT 6.2/i', $ua)){
    $os="Windows 8";}
elseif(preg_match('/Windows NT 4.0/i',$ua) || preg_match('/WinNT4.0/i',$ua)){
    $os="Windows NT 4.0";}
elseif(preg_match('/Windows NT/i',$ua) || preg_match('/WinNT/i',$ua)){
    $os="Windows NT";}
elseif(preg_match('/Windows CE/i',$ua)){
    $os="Windows CE";}
elseif(preg_match('/ipad/i',$ua)){
    $os="iPad";}
elseif(preg_match('/Touch/i',$ua)){
    $os="Touchw";}
elseif(preg_match('/Symbian/i',$ua) || preg_match('/SymbOS/i',$ua)){
    $os="Symbian OS";}
elseif (preg_match('/iPhone/i', $ua)) {
    $os="iPhone";}
elseif(preg_match('/PalmOS/i',$ua)){
    $os="Palm OS";}
elseif(preg_match('/QtEmbedded/i',$ua)){
    $os="Qtopia";}
elseif(preg_match('/Ubuntu/i',$ua)){
    $os="Ubuntu Linux";}
elseif(preg_match('/Gentoo/i',$ua)){
    $os="Gentoo Linux";}
elseif(preg_match('/Fedora/i',$ua)){
    $os="Fedora Linux";}
elseif(preg_match('/FreeBSD/i',$ua)){
    $os="FreeBSD";}
elseif(preg_match('/NetBSD/i',$ua)){
    $os="NetBSD";}
elseif(preg_match('/OpenBSD/i',$ua)){
    $os="OpenBSD";}
elseif(preg_match('/SunOS/i',$ua)){
    $os="SunOS";}
elseif(preg_match('/Linux/i',$ua)){
    $os="Linux";}
elseif(preg_match('/Mac OS X/i',$ua)){
    $os="Mac OS X";}
elseif(preg_match('/Macintosh/i',$ua)){
    $os="Mac OS";}
elseif(preg_match('/Unix/i',$ua)){
    $os="Unix";}
elseif(preg_match('#Nokia([a-zA-Z0-9.]+)#i',$ua,$matches)){
    $os="Nokia".$matches[1];}
elseif(preg_match('/Mac OS X/i',$ua)){
    $os="Mac OS X";}
else{
    $os='未知操作系统';
}
//开始解析浏览器  
if(preg_match('#(Camino|Chimera)[ /]([a-zA-Z0-9.]+)#i',$ua,$matches)){
    $browser = 'Camino '.$matches[2];}
elseif(preg_match('#SE 2([a-zA-Z0-9.]+)#i',$ua,$matches)){
    $browser='搜狗浏览器 2'.$matches[1];}
elseif(preg_match('#360([a-zA-Z0-9.]+)#i',$ua,$matches)){
    $browser='360浏览器 '.$matches[1];}
elseif (preg_match('#Maxthon( |\/)([a-zA-Z0-9.]+)#i',$ua,$matches)){
    $browser='Maxthon '.$matches[2];}
elseif (preg_match('#Chrome/([a-zA-Z0-9.]+)#i',$ua,$matches)){
    $browser='Chrome '.$matches[1];}
elseif (preg_match('#Safari/([a-zA-Z0-9.]+)#i',$ua,$matches)){
    $browser='Safari '.$matches[1];}
elseif(preg_match('#opera mini#i', $ua)) {
    $browser='Opera Mini '.$matches[1];}
elseif(preg_match('#Opera.([a-zA-Z0-9.]+)#i',$ua,$matches)){
    $browser='Opera '.$matches[1];}
elseif(preg_match('#(j2me|midp)#i', $ua)) {
    $browser="J2ME/MIDP Browser";}
elseif(preg_match('/GreenBrowser/i', $ua)){
    $browser='GreenBrowser';}
elseif (preg_match('#TencentTraveler ([a-zA-Z0-9.]+)#i',$ua,$matches)){
    $browser='腾讯TT浏览器 '.$matches[1];}
elseif(preg_match('#UCWEB([a-zA-Z0-9.]+)#i',$ua,$matches)){
    $browser='UCWEB '.$matches[1];}
elseif(preg_match('#MSIE ([a-zA-Z0-9.]+)#i',$ua,$matches)){
    $browser='Internet Explorer '.$matches[1];}
elseif(preg_match('#avantbrowser.com#i',$ua)){
    $browser='Avant Browser';}
elseif(preg_match('#PHP#', $ua, $matches)){
    $browser='PHP';}
elseif(preg_match('#danger hiptop#i',$ua,$matches)){
    $browser='Danger HipTop';}
elseif(preg_match('#Shiira[/]([a-zA-Z0-9.]+)#i',$ua,$matches)){
    $browser='Shiira '.$matches[1];}
elseif(preg_match('#Dillo[ /]([a-zA-Z0-9.]+)#i',$ua,$matches)){
    $browser='Dillo '.$matches[1];}
elseif(preg_match('#Epiphany/([a-zA-Z0-9.]+)#i',$ua,$matches)){
    $browser='Epiphany '.$matches[1];}
elseif(preg_match('#UP.Browser/([a-zA-Z0-9.]+)#i',$ua,$matches)){
    $browser='Openwave UP.Browser '.$matches[1];}
elseif(preg_match('#DoCoMo/([a-zA-Z0-9.]+)#i',$ua,$matches)){
    $browser='DoCoMo '.$matches[1];}
elseif(preg_match('#(Firefox|Phoenix|Firebird|BonEcho|GranParadiso|Minefield|Iceweasel)/([a-zA-Z0-9.]+)#i',$ua,$matches)){
    $browser='Firefox '.$matches[2];}
elseif(preg_match('#(SeaMonkey)/([a-zA-Z0-9.]+)#i',$ua,$matches)){
    $browser='Mozilla SeaMonkey '.$matches[2];}
elseif(preg_match('#Kazehakase/([a-zA-Z0-9.]+)#i',$ua,$matches)){
    $browser='Kazehakase '.$matches[1];}
else{$browser='未知浏览器';
}
return $os." | ".$browser;
}

部分主题提供给用户编辑的文件可能不是这个,具体情况大家可以咨询下主题开发者。

mo_comments_list.php文件修改

在你主题中相要添加这些标识的位置输出以下代码即可:

echo user_agent($comment->comment_agent);

对于我目前使用的DUX主题来说是将代码添加到了mo_comments_list.php中,大部分主题可能需要添加到comments.php这个文件中去,具体添加位置大家可自行选择或者咨询下主题开发者。

当然,如果你不喜欢代码版,或者代码版的操作起来有困难可以尝试使用WP-UserAgent这款插件,这款插件操作选项很多并且支持以图标的形式显示标识,但是对于这种功能性的插件,个人还是建议能少装一个是一个,毕竟多多少少会对网站速度有所影响。

 

官网下载https://wordpress.org/plugins/wp-useragent/