刘道远 發表於 2023-11-21 00:00:00

WordPress评论中禁止HTML代码显示的方法

<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
本文实例讲述了WordPress评论中禁止HTML代码显示的方法。分享给大家供大家参考。具体分析如下:</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
使用WordPress的朋友会发现如果我们开户了博客评论会经常看到大量的垃圾广告,带连接了,那么我们要如何禁止用户输入html标签原样输出,而不显示呢,下面我来给大家介绍.</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
html标题无非就是由“&lt;”、“&gt;”组成了,我们可以反它转换成“&amp;lt;”、“&amp;gt;”,这样通过HTML编译,自动变成了“&lt;”、“&gt;” 我们也可以直接替换掉了</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
找到一国外人的代码,搞定了,不过不一定他是原作者,在functions.php的PHP代码里加入如下代码:<br>
 </p>
<div style='margin: 3px auto 0px; padding: 0px 3px; outline: none; line-height: 21.6px; clear: both; border-width: 1px; border-style: solid; border-color: rgb(0, 153, 204); background: rgb(246, 251, 255); overflow: hidden; font-family: tahoma, arial, "Microsoft YaHei";'>
<div style="margin: 0px; padding: 0px; outline: none; float: right; line-height: 25.2px; font-size: 14px;">
<span style="line-height: 25.2px; cursor: pointer;"><u>复制代码</u></span>
</div>
代码如下:</div>
<div id="phpcode1" style='margin: 0px auto 3px; padding: 0px 3px; outline: none; line-height: 25.2px; font-size: 14px; clear: both; border-right: 1px solid rgb(0, 153, 204); background: rgb(221, 237, 251); overflow: hidden; border-left: 1px solid rgb(0, 153, 204); word-break: break-all; border-bottom: 1px solid rgb(0, 153, 204); word-wrap: break-word; font-family: tahoma, arial, "Microsoft YaHei";'>
//禁用wordpress评论html代码 <br>
// This will occur when the comment is posted <br>
function plc_comment_post( $incoming_comment ) { <br>
// convert everything in a comment to display literally <br>
$incoming_comment['comment_content'] = htmlspecialchars($incoming_comment['comment_content']); <br>
// the one exception is single quotes, which cannot be #039; because WordPress marks it as spam <br>
$incoming_comment['comment_content'] = str_replace( "'", '&amp;apos;', $incoming_comment['comment_content'] ); <br>
return( $incoming_comment ); <br>
} <br>
// This will occur before a comment is displayed <br>
function plc_comment_display( $comment_to_display ) { <br>
// Put the single quotes back in <br>
$comment_to_display = str_replace( '&amp;apos;', "'", $comment_to_display ); <br>
return $comment_to_display; <br>
} <br>
add_filter( 'preprocess_comment', 'plc_comment_post', '', 1); <br>
add_filter( 'comment_text', 'plc_comment_display', '', 1); <br>
add_filter( 'comment_text_rss', 'plc_comment_display', '', 1); <br>
add_filter( 'comment_excerpt', 'plc_comment_display', '', 1);</div>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
 </p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
希望本文所述对大家的WordPress建站有所帮助。</p>
頁: [1]
查看完整版本: WordPress评论中禁止HTML代码显示的方法