How to Disable HTML in WordPress Comments

How to Disable HTML in WordPress Comments

Every coin has two sides, so does the tags in comments. According to the design, WordPress allows visitors to add HTML tags which can be certain links, style text or tables. By using those tags, the comments can be richer in style and better to convey meanings, which can be very beneficial for visitors.

However, there are often lots of spam containing in those tags, which are used to do adverting and then mislead your visitors. Therefore, to better prevent comment spam, it is quite necessary to disable HTML.

This tutorial is going to introduce two approaches to disable HTML in WordPress comments. First of all, the tutorial will show you how to disable HTML manually, which means by codes. Then, we will display three plugins that can save your troubles in setting.

Disable HTML Manually

Disabling manually is actually very easy to operate. First of all, you are supposed to log in your dashboard, and go to Appearance and navigate to your theme directory. Next, open functions.php file under your current theme, and add the code below.


function hr360_comment_post( $newComment ) 
{
	$newComment['comment_content'] = htmlspecialchars($newComment['comment_content']);
	$newComment['comment_content'] = str_replace( "'", ''', $newComment['comment_content'] );
	return $newComment;
}

function hr360_comment_display( $comment ) 
{
	return str_replace( ''', "'", $comment );
}

add_filter( 'preprocess_comment', 'hr360_comment_post', '', 1 );
add_filter( 'comment_text', 'hr360_comment_display', '', 1 );

After those steps, HTML in your comments will be disabled automatically. Then all your comments will include no more tags but to present plain texts.

Disable HTML by Using Plugin

If you find those steps are hard to follow or you are tired of those settings, there are another choice for you: disabling HTML by using a plugin. This tutorial will present three plugins so that you can choose what you prefer.

Peter’s Literal Comments

This plugin is developed by Peter Keung. And now it has updated to version 1.0.2. The plugin is designed only for one purpose – to help users disable the HTML tags in comments. Besides, you need to keep it in mind that the plugin is compatible to at least WordPress 2.5 and up to 4.1.1.

Remove HTML Comments

Remove HTML CommentsThe plugin is developed by a WordPress user codiweb. It is simple to use and light-weight. But as the plugin has not been updated for one year, you should be careful when using it, and there might be compatibility issues.

Advanced Settings

Excepting for the two pugins above, there is also another essential plugin named Advanced Settings. Comparing to the two plugins above, this plugin is more powerful with many functions apart from disabling HTML, such as post type management, removing widget system, inserting Google analytics code and so on. Because this plugin is under updating, there are more functions to come.

Besides, all those plugins are available in WordPress plugin directory, so you just need to search the plugins and activate them. Comparing to disabling HTML manually, using plugin seems to be more convenient because you don’t need to follow any steps or code any codes.

Conclusion

All in all, both methods are easy to operate for beginners. If you have any problems with those steps, just practice several times and then everything will be fine in your comments.