Replacing HTML characters with entities
Comments are a great form of discussion for any WordPress blog or website. Many comments are used to create or induce conversations – expanding blog posts beyond epic proportions, and a lot of comment systems are used to provide support for products (among a million other uses for comments). But a lot of the time WordPress comments are filled with spam.
All WordPress blogs and websites are attacked by a large amount of spam comments daily and without the use of properly configured WordPress plugins (etc.), spam comments are always going to happen to everyone with a WordPress website. I repeat, spam comments will always happen to everyone with a WordPress website, from big companies to small website’s which only have 1 sample post.
Spam comments will never leave unless you properly filter them, but this post is not about filtering spam comments, it’s about changing HTML characters to HTML entities inside WordPress comments.
You can moderate comments and allow them to only appear on your WordPress website with administrative approval, but that’s not a solution for everyone. Some WordPress users would like to allow and provoke the use of their website’s comment system and making their users/visitors wait for their comments to be approved by a moderator is a flawed process.
This is why we have provided an easy PHP function to turn HTML characters into HTML entities in WordPress comments.
Why replace HTML characters with HTML entities?

Replacing characters with entities will make sure spam links are not “clickable” and appear as spam before they are rightfully moderated and removed by the WordPress site administrator.
1. Example: A spammer uses an <a> tag hypertext reference (href) to anchor their website
HTML Characters
<a href=”http://spammer.com/baseball-cards”>Buy baseball cards</a>
In comments this looks like: Buy baseball cards
HTML Entities
When translated from character to entity the spam comment now looks like this:
< ª + " http://spammer.com/baseball-cards " > Buy baseball cards < / ª >
How to change HTML characters to HTML entities
Copy and paste the code below into your functions.php file.
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_d isplay ); 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);
1 Comment