Nana主题评论框增加多功能工具条教程 | 珊瑚贝

前言:之前本博客一直使用的是畅言的评论框,看好的是它的易维护性。然而随着博主的水平提高,也越来越爱瞎折腾。畅言的成品评论框已不能满足我的需求,遂又换回了wordpress的原生评论框。

Nana主题自带的评论框已经集成了垃圾评论过滤、插入表情、评论邮件提醒等功能,但我测试了一下,似乎存在一个xss漏洞(又经过测试发现游客评论并不会触发)。在填补这个漏洞的同时,又顺手给评论框加了个工具条,使得评论的玩法更丰富。在这里顺带放出相关教程。

注:本教程仅针对Nana主题,其它主题虽异曲同工,但需要改动才能适配。具体的自己尝试。

Nana主题评论框增加多功能工具条教程

1、打开Nana主题的目录(/wp-content/themes/Nana),新建一个 css 文件,命名为 “comment-tools.css”,往里面贴入如下代码:

内容太长,已被折叠。点击展开查看
  1. /**
  2. * WordPress 评论多功能工具条css部分
  3. * 修改、完善自网络代码 By 孟坤博客
  4. * 文章地址: https://mkblog.cn/401/
  5. **/
  6. /******************/
  7. /*以下为针对评论内容的修正*/
  8. /******************/
  9. /*去除 取消回复 的加粗*/
  10. #cancel-comment-reply-link{
  11.     font-weight:normal;
  12. }
  13. /*昵称及等级不要加粗*/
  14. .comment-author .duzhe,.comment-author .dengji{
  15.     font-weight:normal;
  16. }
  17. /*楼层不要加粗*/
  18. .comment-aux .floor{
  19.     font-weight:normal;
  20. }
  21. /*评论内容加黑*/
  22. .comment-author p{
  23.     color#000;
  24. }
  25. /*超链接显示为蓝色*/
  26. .comment-author .comment-t-a{
  27.     color#0DAAEA;
  28. }
  29. /*倾斜文字*/
  30. .comment-author .comment-t-i{
  31.     font-styleitalic;
  32. }
  33. /*评论中引用的内容*/
  34. .comment-author .comment-t-blockquote{
  35.     padding10px;
  36.     margin10px 10px 10px 60px;
  37.     widthauto;
  38. }
  39. /*评论中引用的内容中的文字段落*/
  40. .comment-author .comment-t-blockquote p{
  41.     padding-left: 0;
  42.     color#666;
  43. }
  44. /*评论图片不要太宽*/
  45. .comment-author .comment-t-img{
  46.     max-width: 100%;
  47. }
  48. /*代码*/
  49. .comment-author .comment-t-pre {
  50.     padding-left60px;
  51. }
  52. /*评论内容垂直居中,同时新加评论预览不会顶在上面*/
  53. .comment-list{
  54.     margin5px 0;
  55. }
  56. /*去掉莫名其妙的阴影*/
  57. #respond{
  58.     box-shadow: none;
  59. }
  60. /******************/
  61. /*以下为评论工具条*/
  62. /******************/
  63. .smiley{    /*针对nana主题:去掉主题自带的表情按钮*/
  64.     displaynone;
  65. }
  66. .smiley-box{    /*针对nana主题:防止在移动端表情框太宽,跑出去了*/
  67.     width: 100%;
  68.     max-width497px;
  69. }
  70. #editor_tools{
  71.     displayinlineblock;
  72.     line-height26px;
  73.     border1px #e0e0e0 solid;
  74.     border-radius: 2px 2px 0 0;
  75.     overflowhidden;
  76.     z-index: 99999;
  77. }
  78. #editor_tools #editor .tools_tip{
  79.     padding-left5px;
  80.     displaynone;  /*如果需要显示文字,请去掉这个*/
  81. }
  82. #editor_tools a{
  83.     color#777;
  84.     displayinlineblock;
  85.     padding: 0 8px;
  86.     height26px;
  87.     min-width30px;
  88.     text-aligncenter;
  89. }
  90. #editor_tools a:hover{
  91.     color#333;
  92.     text-decorationnone;
  93. }
  94. #fontcolor{
  95.     displaynone;
  96.     line-height20px;
  97.     border2px #e0e0e0 solid;
  98.     z-index: 99999;
  99.     margin-bottom10px;
  100.     padding4px 0 0 4px;
  101.     max-width376px;
  102. }
  103. #fontcolor a{
  104.     displayinlineblock;
  105.     height16px;
  106.     width16px;
  107. }

2、再打开 Nana 主题目录下的 js 文件夹,在里面新建一个js文件,命名为 “comment-tools.js” ,并往里面贴入以下代码:

内容太长,已被折叠。点击展开查看
  1. /**
  2. * WordPress 评论多功能工具条css部分
  3. * 修改、完善自网络代码 By 孟坤博客
  4. * 文章地址: https://mkblog.cn/401/
  5. **/
  6. $(function () {
  7.     $(“#comment-smiley”).click(function(){   //点击弹出表情
  8.         $(‘.smiley-box’).animate({
  9.             opacity: ‘toggle’,
  10.             left: ’50px’
  11.         }, 1000).animate({
  12.             left: ’10px’
  13.         }, ‘fast’);
  14.         return false;
  15.     });
  16.     $(“#font-color”).click(function(){   //点击弹出颜色
  17.         $(“#fontcolor”).toggle(500);
  18.     });
  19.     $(“#comment”).click(function(){   //点击输入框隐藏颜色
  20.         $(“#fontcolor”).hide(500);
  21.     });
  22. });
  23. jQuery(function() {
  24.     function addEditor(a, b, c) {
  25.         if (document.selection) {
  26.             a.focus();
  27.             sel = document.selection.createRange();
  28.             c ? sel.text = b + sel.text + c: sel.text = b;
  29.             a.focus()
  30.         } else if (a.selectionStart || a.selectionStart == ‘0’) {
  31.             var d = a.selectionStart;
  32.             var e = a.selectionEnd;
  33.             var f = e;
  34.             c ? a.value = a.value.substring(0, d) + b + a.value.substring(d, e) + c + a.value.substring(e, a.value.length) : a.value = a.value.substring(0, d) + b + a.value.substring(e, a.value.length);
  35.             c ? f += b.length + c.length: f += b.length – e + d;
  36.             if (d == e && c) f -= c.length;
  37.             a.focus();
  38.             a.selectionStart = f;
  39.             a.selectionEnd = f
  40.         } else {
  41.             a.value += b + c;
  42.             a.focus()
  43.         }
  44.     }
  45.     var myDate = new Date();
  46.     var mytime=myDate.toLocaleTimeString()
  47.     var g = document.getElementById(‘comment’) || 0;
  48.     var h = {
  49.         strong: function() {  //粗体文字
  50.             addEditor(g, ‘[b]’, ‘[/b]’);
  51.         },
  52.         em: function() {  //斜体文字
  53.             addEditor(g, ‘[i]’, ‘[/i]’);
  54.         },
  55.         del: function() {  //删除线
  56.             addEditor(g, ‘[del]’, ‘[/del]’);
  57.         },
  58.         underline: function() {  //下划线
  59.             addEditor(g, ‘[u]’, ‘[/u]’);
  60.         },
  61.         quote: function() {  //引用
  62.             addEditor(g, ‘[blockquote]’, ‘[/blockquote]’);
  63.         },
  64.         ahref: function() {
  65.             var a = prompt(‘请输入链接地址’, ‘http://’);  
  66.             var b = prompt(‘请输入链接内容’, );
  67.             if (a) {  //超链接
  68.                 addEditor(g, ‘[url=’ + a + ‘]’ + b + ‘[/url]’, );
  69.             }
  70.         },
  71.         img: function() {  //图片
  72.             var a = prompt(‘请输入图片地址’, ‘http://’);  
  73.             if (a) {
  74.                 addEditor(g, ‘‘, );
  75.             }
  76.         },
  77.         sign: function() {  //签到
  78.             addEditor(g, ‘好文帮顶 & 顺便留个名~时间:’ + mytime, );
  79.         },
  80.         code: function() {  //代码段
  81.             addEditor(g, ‘[pre]’, ‘[/pre]’);
  82.         },
  83.         red: function() {  //各种颜色
  84.             addEditor(g, ‘[color=red]’, ‘[/color]’);
  85.         },
  86.         fuchsia: function() {
  87.             addEditor(g, ‘[color=fuchsia]’, ‘[/color]’);
  88.         },
  89.         purple: function() {
  90.         addEditor(g, ‘[color=purple]’, ‘[/color]’);
  91.         },
  92.         orange: function() {
  93.             addEditor(g, ‘[color=orange]’, ‘[/color]’);
  94.         },
  95.         yellow: function() {
  96.         addEditor(g, ‘[color=yellow]’, ‘[/color]’);
  97.         },
  98.         olive: function() {
  99.         addEditor(g, ‘[color=olive]’, ‘[/color]’);
  100.         },
  101.         lime: function() {
  102.         addEditor(g, ‘[color=lime]’, ‘[/color]’);
  103.         },
  104.         maroon: function() {
  105.         addEditor(g, ‘[color=maroon]’, ‘[/color]’);
  106.         },
  107.         aqua: function() {
  108.         addEditor(g, ‘[color=aqua]’, ‘[/color]’);
  109.         },
  110.         teal: function() {
  111.           addEditor(g, ‘[color=teal]’, ‘[/color]’);
  112.         },
  113.         green: function() {
  114.         addEditor(g, ‘[color=green]’, ‘[/color]’);
  115.         },
  116.         blue: function() {
  117.             addEditor(g, ‘[color=blue]’, ‘[/color]’);
  118.         },
  119.         navy: function() {
  120.             addEditor(g, ‘[color=navy]’, ‘[/color]’);
  121.         },
  122.         gray: function() {
  123.             addEditor(g, ‘[color=gray]’, ‘[/color]’);
  124.         },
  125.         deepskyblue: function() {
  126.             addEditor(g, ‘[color=deepskyblue]’, ‘[/color]’);
  127.         },
  128.         gold: function() {
  129.             addEditor(g, ‘[color=gold]’, ‘[/color]’);
  130.         },      silver: function() {
  131.             addEditor(g, ‘[color=silver]’, ‘[/color]’);
  132.         },
  133.         black: function() {
  134.             addEditor(g, ‘[color=black]’, ‘[/color]’);
  135.         }
  136.     };
  137.     window[‘SIMPALED’] = {};
  138.     window[‘SIMPALED’][‘Editor’] = h
  139. });

3、打开Nana主题目录下的 inc 文件夹,在里面新建一个php文件,命名为 “comment-tools.php” 并在里面贴入如下代码:

内容太长,已被折叠。点击展开查看
  1. <!–  评论框工具条  inc->comment-tools.php  –>
  2. <div id=“editor_tools”>
  3.     <div id=“editor”>
  4.         <a href=“javascript:;” id=“comment-smiley” title=“点击插入表情”>
  5.             <i class=“fa fa-smile-o” aria-hidden=“true”></i><span class=“tools_tip”>表情</span>
  6.         </a>
  7.         <a href=“javascript:SIMPALED.Editor.img()” title=“点击插入图片”>
  8.             <i class=“fa fa-picture-o” aria-hidden=“true”></i><span class=“tools_tip”>图片</span>
  9.         </a>
  10.         <a href=“javascript:SIMPALED.Editor.ahref()” title=“点击插入超链接”>
  11.             <i class=“fa fa-link” aria-hidden=“true”></i><span class=“tools_tip”>链接</span>
  12.         </a>
  13.         <a href=“javascript:;” id=“font-color” title=“点击插入彩色文字”>
  14.             <i class=“fa fa-eyedropper” aria-hidden=“true”></i><span class=“tools_tip”>颜色</span>
  15.         </a>
  16.         <a href=“javascript:SIMPALED.Editor.strong()” title=“点击插入粗体文字”>
  17.             <i class=“fa fa-bold” aria-hidden=“true”></i><span class=“tools_tip”>加粗</span>
  18.         </a>
  19.         <a href=“javascript:SIMPALED.Editor.em()” title=“点击插入斜体文字”>
  20.             <i class=“fa fa-italic” aria-hidden=“true”></i><span class=“tools_tip”>倾斜</span>
  21.         </a>
  22.         <a href=“javascript:SIMPALED.Editor.del()” title=“点击插入删除线”>
  23.             <i class=“fa fa-strikethrough” aria-hidden=“true”></i><span class=“tools_tip”>删除线</span>
  24.         </a>
  25.         <a href=“javascript:SIMPALED.Editor.underline()” title=“点击插入下划线”>
  26.             <i class=“fa fa-underline” aria-hidden=“true”></i><span class=“tools_tip”>下划线</span>
  27.         </a>
  28.         <a href=“javascript:SIMPALED.Editor.quote()” title=“点击插入引用内容”>
  29.             <i class=“fa fa-quote-left” aria-hidden=“true”></i><span class=“tools_tip”>引用</span>
  30.         </a>
  31.         <a href=“javascript:SIMPALED.Editor.code()” title=“点击插入代码段”>
  32.             <i class=“fa fa-code” aria-hidden=“true”></i><span class=“tools_tip”>代码</span>
  33.         </a>
  34.         <a href=“javascript:SIMPALED.Editor.sign()” title=“点击插入签到代码”>
  35.             <i class=“fa fa-check-circle” aria-hidden=“true”></i><span class=“tools_tip”>签到</span>
  36.         </a>
  37.     </div>
  38. </div>
  39. <div id=“fontcolor”>
  40.     <a href=“javascript:SIMPALED.Editor.red()” style=“background-color: red”></a>
  41.     <a href=“javascript:SIMPALED.Editor.fuchsia()” style=“background-color: fuchsia”></a>
  42.     <a href=“javascript:SIMPALED.Editor.purple()” style=“background-color: purple”></a>
  43.     <a href=“javascript:SIMPALED.Editor.orange()” style=“background-color: orange”></a>
  44.     <a href=“javascript:SIMPALED.Editor.yellow()” style=“background-color: yellow”></a>
  45.     <a href=“javascript:SIMPALED.Editor.gold()” style=“background-color: gold”></a>
  46.     <a href=“javascript:SIMPALED.Editor.olive()” style=“background-color: olive”></a>
  47.     <a href=“javascript:SIMPALED.Editor.lime()” style=“background-color: lime”></a>
  48.     <a href=“javascript:SIMPALED.Editor.aqua()” style=“background-color: aqua”></a>
  49.     <a href=“javascript:SIMPALED.Editor.deepskyblue()” style=“background-color: deepskyblue”></a>
  50.     <a href=“javascript:SIMPALED.Editor.teal()” style=“background-color: teal”></a>
  51.     <a href=“javascript:SIMPALED.Editor.green()” style=“background-color: green”></a>
  52.     <a href=“javascript:SIMPALED.Editor.blue()” style=“background-color: blue”></a>
  53.     <a href=“javascript:SIMPALED.Editor.maroon()” style=“background-color: maroon”></a>
  54.     <a href=“javascript:SIMPALED.Editor.navy()” style=“background-color: navy”></a>
  55.     <a href=“javascript:SIMPALED.Editor.gray()” style=“background-color: gray”></a>
  56.     <a href=“javascript:SIMPALED.Editor.silver()” style=“background-color: silver”></a>
  57.     <a href=“javascript:SIMPALED.Editor.black()” style=“background-color: black”></a>
  58. </div>
  59. <!–评论框工具条核心js文件–>
  60. <script src=“<?php bloginfo(‘template_directory’); ?>/js/comment-tools.js”></script>
  61. <!–评论框工具条核心css文件–>
  62. <link rel=“stylesheet” href=“<?php bloginfo(‘template_directory’); ?>/comment-tools.css” type=“text/css” media=“all”>

4、打开Nana主题目录下的“comments.php”,找到第70行

  1. <p class=“smiley-box”>
  2.     <?php get_template_part( ‘inc/smiley’ ); ?>
  3. </p>

在它的后面添加

  1. <p class=“comment-tools-box”>
  2.      <?php get_template_part( ‘inc/comment-tools’ ); ?>
  3. </p>

如图所示:
Nana主题评论框增加多功能工具条教程

5、最后一步!打开Nana主题目录下的“functions.php”,拉到最下面,在最后一行的“ ?> ”前面加入以下代码:

  1. /**
  2. * WordPress 评论多功能工具条后台处理部分
  3. * 编写 By 孟坤博客
  4. * 文章地址: https://mkblog.cn/401/
  5. **/
  6. function comment_code_escape( $incoming_comment ) {
  7.     $incoming_comment = str_replace(array(‘<‘, ‘>’), array(‘&lt;’, ‘&gt;’), $incoming_comment);//第一步就去掉尖括号,防止xss htmlspecialchars($incoming_comment, ENT_NOQUOTES);
  8.     $incoming_comment = preg_replace(‘/\[b\](.*?)\[\/b\]/is’,'<b class=“comment-t-b”>$1</b>’, $incoming_comment); //转换粗体
  9.     $incoming_comment = preg_replace(‘/\[i\](.*?)\[\/i\]/is’,'<i class=“comment-t-i”>$1</i>’, $incoming_comment); //转换斜体
  10.     $incoming_comment = preg_replace(‘/\[u\](.*?)\[\/u\]/is’,'<u class=“comment-t-u”>$1</u>’, $incoming_comment); //转换下划线
  11.     $incoming_comment = preg_replace(‘/\[del\](.*?)\[\/del\]/is’,'<del class=“comment-t-del”>$1</del>’, $incoming_comment); //转换删除线
  12.     $incoming_comment = preg_replace(‘/\[pre\](.*?)\[\/pre\]/is’,'<pre class=“comment-t-pre”>$1</pre>’, $incoming_comment); //转换代码
  13.     $incoming_comment = preg_replace(‘/\[blockquote\](.*?)\[\/blockquote\]/is’,'<blockquote class=“comment-t-blockquote”>$1</blockquote>’, $incoming_comment); //转换引用
  14.     $incoming_comment = preg_replace(‘/\[color=([\w|#]*?)\](.*?)\[\/color\]/is’,'<span style=“color: $1” class=“comment-t-color”>$2</span>’, $incoming_comment); //转换颜色
  15.     $incoming_comment = preg_replace(‘/\[url=(.*?)\](.*?)\[\/url\]/is’,'<a href=“$1” target=“_blank” class=“comment-t-a”>$2</a>’, $incoming_comment); //转换超链接
  16.     $incoming_comment = preg_replace(‘/\(.*?)\[\/img\]/is’,'<a href=“$1” class=“cboxElement comment-t-img-a” rel=“example_group”><img src=“$1” class=“comment-t-img aligncenter size-full wp-image-393 box-hide box-show”></a>’, $incoming_comment); //转换图片
  17.     return $incoming_comment;
  18. }
  19. //用户在进行评论时就进行处理(即先进行替换,再写进数据库)
  20. //add_filter( ‘preprocess_comment’, ‘comment_code_escape’ );    
  21. //在评论显示出来时进行处理(推荐) (即评论按原样写进数据库,再读出来时进行替换)
  22. add_filter( ‘comment_text’, ‘comment_code_escape’ );  //显示时替换
  23. add_filter( ‘comment_text_rss’, ‘comment_code_escape’ );  //显示时替换

注:这里提供了两种后台替换方式,各有利弊,自己权衡选择。

为了方便修改,我已将上述“comment-tools.css”、“comment-tools.js”和“comment-tools.php”打包
百度网盘 永硕E盘

2016-12-5更新

我采用的是评论输出再转义替换的方法,更改完后发现wordpress会自动为链接加上超链接再输出。因为我在上面的代码把尖括号都作了转义,所以最终输出的链接变成了如下这个样子:

Nana主题评论框增加多功能工具条教程

解决办法是将尖括号转义单独提取出来,放在评论“入库”时处理,然后去掉wordpress的评论自动解析超链接功能。最终的 function.php 里的代码如下:

  1. /**
  2. * WordPress 评论多功能工具条后台处理部分
  3. * 编写 By 孟坤博客
  4. * 文章地址: https://mkblog.cn/401/
  5. **/
  6. function comment_code_escape( $incoming_comment ) {
  7.     $incoming_comment = preg_replace(‘/\[b\](.*?)\[\/b\]/is’,'<b class=“comment-t-b”>$1</b>’, $incoming_comment); //转换粗体
  8.     $incoming_comment = preg_replace(‘/\[i\](.*?)\[\/i\]/is’,'<i class=“comment-t-i”>$1</i>’, $incoming_comment); //转换斜体
  9.     $incoming_comment = preg_replace(‘/\[u\](.*?)\[\/u\]/is’,'<u class=“comment-t-u”>$1</u>’, $incoming_comment); //转换下划线
  10.     $incoming_comment = preg_replace(‘/\[del\](.*?)\[\/del\]/is’,'<del class=“comment-t-del”>$1</del>’, $incoming_comment); //转换删除线
  11.     $incoming_comment = preg_replace(‘/\[pre\](.*?)\[\/pre\]/is’,'<pre class=“comment-t-pre”>$1</pre>’, $incoming_comment); //转换代码
  12.     $incoming_comment = preg_replace(‘/\[blockquote\](.*?)\[\/blockquote\]/is’,'<blockquote class=“comment-t-blockquote”>$1</blockquote>’, $incoming_comment); //转换引用
  13.     $incoming_comment = preg_replace(‘/\[color=([\w|#]*?)\](.*?)\[\/color\]/is’,'<span style=“color: $1” class=“comment-t-color”>$2</span>’, $incoming_comment); //转换颜色
  14.     $incoming_comment = preg_replace(‘/\[url=(.*?)\](.*?)\[\/url\]/is’,'<a href=“$1” target=“_blank” class=“comment-t-a”>$2</a>’, $incoming_comment); //转换超链接
  15.     $incoming_comment = preg_replace(‘/\(.*?)\[\/img\]/is’,'<a href=“$1” class=“cboxElement comment-t-img-a” rel=“example_group”><img src=“$1” class=“comment-t-img aligncenter size-full wp-image-393 box-hide box-show”></a>’, $incoming_comment); //转换图片
  16.     return $incoming_comment;
  17. }
  18. //在评论显示出来时进行处理
  19. add_filter( ‘comment_text’, ‘comment_code_escape’ );  //评论显示时替换
  20. add_filter( ‘comment_text_rss’, ‘comment_code_escape’ );  //rss显示时替换
  21. /**
  22. * WordPress 评论尖括号处理部分
  23. * 编写 By 孟坤博客
  24. **/
  25. function mk_comment_escape( $incoming_comment ) {
  26.     $incoming_comment = str_replace(array(‘<‘, ‘>’), array(‘&lt;’, ‘&gt;’), $incoming_comment);//去掉尖括号,防止xss
  27.     return $incoming_comment;
  28. }
  29. add_filter( ‘preprocess_comment’, ‘mk_comment_escape’ );    //评论写入时就进行转义
  30. remove_filter( ‘comment_text’, ‘make_clickable’,  9 );  //去除wordpress评论链接自动解析

2016-12-17更新

请参照5楼评论

参考资料

[1]. 给wordpress评论处添加实用工具.爱找主题 http://www.2zzt.com/jcandcj/7397.html

[2]. WordPress开启颜色评论但不造成XSS漏洞的小方法.张戈博客 https://zhangge.net/5010.html

来源:https://mkblog.cn/401/

微信公众号
手机浏览(小程序)
0
分享到:
没有账号? 忘记密码?