前兩天小編接到客户反應,網站收到很多莫名其妙的評論,評論的內容是一大堆 html 代碼,在前台顯示的其實是一段廣告代碼,為此客户非常煩惱,其實遇到這種情況直接在評論提交的時候把 html 代碼過濾下即可,下面是小編當時的處理方法:
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);
只需要將以上代碼加入到當前使用的主題的 functions.php 文件即可,這樣 WordPress 就會在收到評論的時候自動把評論中的 html 代碼過濾掉~