> 由于想给博客加一个文章评论显示 UserAgent 功能,在网上搜寻并尝试了 UserAgent 插件,但是并不如意。一是因为他太过臃肿,虽然几乎可以识别市面上所有的 OS 和浏览器,但是我们常用的也就几个而已,大多数都用不到,二是图标太老旧了,而且清晰度很低,在博客上显得突兀且不美观。—-来自左岸博客的引用(传送门)
代码实现评论显示UserAgent
林三通过试用,觉得左岸同学的方法比较适合自己,因为手动添加函数意味着更自由的位置和样式显示。
1、首先找到主题根目录,打开 functions.php 文件,在函数区域(不是最顶端)粘贴下面的代码:
1. // 获取浏览器信息
2. function getBrowser($agent)
3. {
4. if (preg_match(‘/MSIE\s([^\s|;]+)/i’, $agent, $regs)) {
5. $outputer = ‘Internet Explore’;
6. } else if (preg_match(‘/FireFox\/([^\s]+)/i’, $agent, $regs)) {
7. $str1 = explode(‘Firefox/’, $regs[0]);
8. $FireFox_vern = explode(‘.’, $str1[1]);
9. $outputer = ‘FireFox’;
10. } else if (preg_match(‘/Maxthon([\d]*)\/([^\s]+)/i’, $agent, $regs)) {
11. $str1 = explode(‘Maxthon/’, $agent);
12. $Maxthon_vern = explode(‘.’, $str1[1]);
13. $outputer = ‘MicroSoft Edge’;
14. } else if (preg_match(‘#360([a-zA-Z0-9.]+)#i’, $agent, $regs)) {
15. $outputer = ‘360 Fast Browser’;
16. } else if (preg_match(‘/Edge([\d]*)\/([^\s]+)/i’, $agent, $regs)) {
17. $str1 = explode(‘Edge/’, $regs[0]);
18. $Edge_vern = explode(‘.’, $str1[1]);
19. $outputer = ‘MicroSoft Edge’;
20. } else if (preg_match(‘/UC/i’, $agent)) {
21. $str1 = explode(‘rowser/’, $agent);
22. $UCBrowser_vern = explode(‘.’, $str1[1]);
23. $outputer = ‘UC Browser’;
24. } else if (preg_match(‘/QQ/i’, $agent, $regs)||preg_match(‘/QQ Browser\/([^\s]+)/i’, $agent, $regs)) {
25. $str1 = explode(‘rowser/’, $agent);
26. $QQ_vern = explode(‘.’, $str1[1]);
27. $outputer = ‘QQ Browser’;
28. } else if (preg_match(‘/UBrowser/i’, $agent, $regs)) {
29. $str1 = explode(‘rowser/’, $agent);
30. $UCBrowser_vern = explode(‘.’, $str1[1]);
31. $outputer = ‘UC Browser’;
32. } else if (preg_match(‘/Opera\s|\//i’, $agent, $regs)) {
33. $outputer = ‘Opera’;
34. } else if (preg_match(‘/Chrome([\d]*)\/([^\s]+)/i’, $agent, $regs)) {
35. $str1 = explode(‘Chrome/’, $agent);
36. $chrome_vern = explode(‘.’, $str1[1]);
37. $outputer = ‘Google Chrome’;
38. } else if (preg_match(‘/safari\/([^\s]+)/i’, $agent, $regs)) {
39. $str1 = explode(‘Version/’, $agent);
40. $safari_vern = explode(‘.’, $str1[1]);
41. $outputer = ‘Safari’;
42. } else{
43. $outputer = ‘Google Chrome’;
44. }
45. echo $outputer;
46. }
47. // 获取操作系统信息
48. function getOs($agent)
49. {
50. $os = false;
51. if (preg_match(‘/win/i’, $agent)) {
52. if (preg_match(‘/nt 6.0/i’, $agent)) {
53. $os = ‘Windows Vista · ’;
54. } else if (preg_match(‘/nt 6.1/i’, $agent)) {
55. $os = ‘Windows 7 · ’;
56. } else if (preg_match(‘/nt 6.2/i’, $agent)) {
57. $os = ‘Windows 8 · ’;
58. } else if(preg_match(‘/nt 6.3/i’, $agent)) {
59. $os = ‘Windows 8.1 · ’;
60. } else if(preg_match(‘/nt 5.1/i’, $agent)) {
61. $os = ‘Windows XP · ’;
62. } else if (preg_match(‘/nt 10.0/i’, $agent)) {
63. $os = ‘Windows 10 · ’;
64. } else{
65. $os = ‘Windows X64 · ’;
66. }
67. } else if (preg_match(‘/android/i’, $agent)) {
68. if (preg_match(‘/android 9/i’, $agent)) {
69. $os = ‘Android Pie · ’;
70. }
71. else if (preg_match(‘/android 8/i’, $agent)) {
72. $os = ‘Android Oreo · ’;
73. }
74. else{
75. $os = ‘Android · ’;
76. }
77. }
78. else if (preg_match(‘/ubuntu/i’, $agent)) {
79. $os = ‘Ubuntu · ’;
80. } else if (preg_match(‘/linux/i’, $agent)) {
81. $os = ‘Linux · ’;
82. } else if (preg_match(‘/iPhone/i’, $agent)) {
83. $os = ‘iPhone · ’;
84. } else if (preg_match(‘/mac/i’, $agent)) {
85. $os = ‘MacOS · ’;
86. }else if (preg_match(‘/fusion/i’, $agent)) {
87. $os = ‘Android · ’;
88. } else {
89. $os = ‘Linux · ’;
90. }
91. echo $os;
92. }
2、comments.php 中找到合适位置(比如评论作者的后面)添加以下代码:
1. <span class=“comment-ua”><?php getOs($comments->agent); ?><?php getBrowser($comments->agent); ?></span>
然后刷新页面就可以看到UA信息显示出来了。林三这里去掉了图标显示,如果想要图标的,请参考左岸博客的原文来操作,还需要复制相应的css代码到你的样式表中(左岸同学使用的图标在cnd上面,速度还是不错的)。
插件实现评论显示UserAgent
UserAgent是一个较好的评论增强插件,可以分别设置图标、文字或图标+文字形式,来显示评论的UA部分。
1、下载插件:https://github.com/ennnnny/typecho 或者使用网盘下载:传送门,提取码 aqe5
2、然后解压上传UserAgent文件夹至主题插件目录并启用,选择你想要的显示效果保存
3、引用,在你想显示的位置上加上这段代码:<?php UserAgent_Plugin::render($this->agent); ?>
请根据自己的模板来判断是使用$this或$comments(如果不清楚,可以都试下),林三试了下,也是可以显示的。