wordpress过滤评论中的html代码实用教程

wordpress过滤评论中的html代码实用教程

最近有童鞋反映,自己的网页中莫名其妙出现了一些广告!查遍主题和插件都没有找到广告代码!最后发现,他的评论中有广告代码,被运行了!

为了解决这个问题,我们可以尝试屏蔽广告代码,就是在用户评论的时候wordpress就会自动过滤评论中的html代码。

将以下代码加入到当前使用的主题的functions.php文件:

function plc_comment_post( $incoming_comment ) {
  
  
        $incoming_comment['comment_content'] = htmlspecialchars($incoming_comment['comment_content']);
  
        $incoming_comment['comment_content'] = str_replace( "'", ''', $incoming_comment['comment_content'] );
  
        return( $incoming_comment );
}
  
function plc_comment_display( $comment_to_display ) {
  
        $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);