我也做了两款主题了,虽然样子都不怎么样,非常简单,但功能基本都满足日常需求。今天分享的让博客评论里的HTML代码无法编译,也算是我搜集到的小技巧之一。
你一定有这种烦恼,求助一些代码,网友好心在评论里留言给你,结果都解析了,非常蛋疼。我之前也考虑过这个问题,而且还问过博客圈几位牛人,他们虽然都跟我说了一下,但是我自己太笨,还是搞不定。我当时的想法就是把评论里的“<”、“>”转换成“<”、“>”,这样通过HTML编译,自动变成了“<”、“>”。但是我当时只想这用jQuery去转,不会PHP。后来看见的方法是用PHP,当然,网上全都是转载的,而且我复制过来还不对,所以继续Google,找到一国外人的代码,搞定了。不过不一定他是原作者。
在functions.php的PHP代码里加入:
//禁用wordpress评论html代码
// This will occur when the comment is posted
function plc_comment_post( $incoming_comment ) {
// convert everything in a comment to display literally
$incoming_comment['comment_content'] = htmlspecialchars($incoming_comment['comment_content']);
// the one exception is single quotes, which cannot be #039; because WordPress marks it as spam
$incoming_comment['comment_content'] = str_replace( "'", ''', $incoming_comment['comment_content'] );
return( $incoming_comment );
}
// This will occur before a comment is displayed
function plc_comment_display( $comment_to_display ) {
// Put the single quotes back in
$comment_to_display = str_replace( ''', "'", $comment_to_display );
return $comment_to_display;
}
add_filter( 'preprocess_comment', 'plc_comment_post', '', 1);
add_filter( 'comment_text', 'plc_comment_display', '', 1);
add_filter( 'comment_text_rss', 'plc_comment_display', '', 1);
add_filter( 'comment_excerpt', 'plc_comment_display', '', 1);
效果可以在我博客评论里留言HTML代码便可以看见啦!这样下次大家给我留言HTML代码,就不怕被编译啦!