Tutorials – How to Disable HTML in WordPress Comments

disablehtmlincomments-180x180
disablehtmlincomments-180x180

By default, WordPress allows certain HTML tags within the comments such as <a> <em> <strong> etc. If you notice a lot of SPAM comments also contain these tags. Most SPAM comments are made by bots and scripts, which are using HTML tags. If you simply disable HTML from your WordPress comments, it can prevent a lot of SPAM. In this tutorial we will show you how you can disable HTML tags in your WordPress comments.

This tutorial will only disable active HTML tags. So someone can still post something like:

&lt;a&gt;&lt;em&gt;&lt;strong&gt;

And it will show up, but the tags will not be functional. So if someone uses the strong tag, it won’t bold the text. Besides not many SPAM bots have time to do this because this way takes up a lot of time and it is not beneficial for them.

All you have to do is simply open your functions.php and add the following code:

01 // This will occur when the comment is posted
02 function plc_comment_post( $incoming_comment ) {
03
04 // convert everything in a comment to display literally
05 $incoming_comment['comment_content'] = htmlspecialchars($incoming_comment['comment_content']);
06
07 // the one exception is single quotes, which cannot be #039; because WordPress marks it as spam
08 $incoming_comment['comment_content'] = str_replace( "'", '&apos;', $incoming_comment['comment_content'] );
09
10 return( $incoming_comment );
11 }
12
13 // This will occur before a comment is displayed
14 function plc_comment_display( $comment_to_display ) {
15
16 // Put the single quotes back in
17 $comment_to_display = str_replace( '&apos;', "'", $comment_to_display );
18
19 return $comment_to_display;

Source – The original author also offers a plugin that you can download from his site.

The reason why this way is better is because it does not require you to change the core files. If you want to edit your core files then you may go to wp-includes/kses.php and edit the codes there. (This is not Recommended, but it is here for the sake of knowledge. (WP Codex for more details)

From http://www.wpbeginner.com/