How to disable HTML in WordPress comments

If you’re allowing your visitors to comment on your articles, by default, they will be able to use some HTML tags to style their witty comments below your post. Usually, your commenters will use those tags to make words bold, italic or they will add links into your comments. If you want only to disable links in comments, be sure to catch our older article.

But if you have a clean website design and a lot of people commenting, those different styles might mess up your layout and your site will stop being so clean, right? And that, besides other reasons, might be just a good enough reason to completely remove HTML from your WordPress comments.

Disable HTML comments in WordPress

Disable HTML in comments:

It isn’t that hard to disable HTML in WP comments. As usual, we will give you the code which you have to copy and paste and the rest will be done by WordPress.

  1. Open functions.php file
  2. Copy and paste the following snippet:
  3. 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);
  4. Save changes

This code will tell your WordPress to take everything in comments literally. So, if a user for example types in “<b>” tag to make his text bold, nothing will happen, except there will be a “<b>” tag displayed in a comment. The exception is single quotes which are recognized as spam by WordPress.

Now that you have made the changes, it would be nice to tell your visitor about the changes – you don’t want your commenters to be angry after they realize their HTML magic isn’t working and that their comment is full of gibberish.

Since you’re still working on comments, you might be interested in separating comments in pages. Also, see how comments from your other pages can help you get more page views.

START YOUR OWN BLOG

This guide is an introduction to mastering the art of blogging. It provides easy to follow steps to start, maintain, and grow your blog.

Read the guide

Leave a Reply

Your email address will not be published. Required fields are marked *