WordPress谈论框显示用户浏览器及操作系统信息

之前网站上共享过一篇 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/

资源下载

此资源为免费资源立刻下载留意:本站资源多为网络收集,如触及版权问题请及时与站长联络,我们会在第一时间内与您洽谈处理。如非特殊阐明,本站一切资源解压密码均为:zibuyu.life。