Sarathlal N

Disable HTML content in WordPress comments

The post comments is consider as a method to increase user interaction in our blogs. When we read certain articles, we can realize that the comments associated with that article have more quality than the content.

But if we have poor comment moderation methods, spammers utilize this commenting system to increase there site SEO by inserting there links in our post comments.

In WordPress, we have amazing plugins and method to avoid automatically generated spam comments. But they have limitations & we want to do manual moderation in almost cases.

In spam comments, we can realize that they are always associated with a web address. They write spam comments in our post comments because they can create HTML links via there comments.

So today we are going to tighten our comments terms & policy. From now, we never allow HTML content in our post comments.

All you have to do is simply open our child theme’s functions.php and add the following code.

// 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;
}

This code snippet will filter HTML from our comments. If spammers can’t link there website, they like to avoid our posts because there intension is only simple link generation.

Got a project in mind? Send me a quick message, and I'll get back to you within 24 hours!.

Recent Posts

  1. Disabling Payment Methods in WooCommerce Based on Conditions
  2. How to Update Product Quantity in WooCommerce Using Custom Code
  3. Dynamically Generating a Table of Contents in WordPress
  4. Direct Checkout in WooCommerce - Add Product to Cart from Checkout Page & Skip Shop, Product, and Cart Pages
  5. Understanding the Impact of git reset --hard Command

Your Questions / Comments

If you found this article interesting, found errors, or just want to discuss about it, please get in touch.