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:
<a><em><strong>
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:
02 | function plc_comment_post( $incoming_comment ) { |
05 | $incoming_comment [ 'comment_content' ] = htmlspecialchars( $incoming_comment [ 'comment_content' ]); |
08 | $incoming_comment [ 'comment_content' ] = str_replace ( "'" , '' ', $incoming_comment[' comment_content'] ); |
10 | return ( $incoming_comment ); |
14 | function plc_comment_display( $comment_to_display ) { |
17 | $comment_to_display = str_replace ( ''' , "'" , $comment_to_display ); |
19 | return $comment_to_display ; |
If you don’t want to manually add this code yourself, then the original author also offers a plugin that you can download. Simply install and activate Peter’s Literal Comments plugin.
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)