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:
// 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;
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)
How to Disable HTML in WordPress Comments
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:
// 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;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)
About Editorial Staff
Editorial Staff at WPBeginner mainly Syed and David.
What's Next
Get WPBeginner Articles in your Inbox:
Related Posts
The more mature you are, the harder at risk you are for getting something undesirable happen to you while overseas. Thanks for revealing your suggestions on this blog.You have good grapes on wordpress thanks for making my problem so easy .
- spam
- offensive
- disagree
- off topic
LikeGreat, by this way one can ensure that nobody drops links in the comment box. I really hate to see people doing that unnecessarily.
- spam
- offensive
- disagree
- off topic
LikeNice piece of code,but if you happen to use thesis like I am,only needs to check a radio button and the html code vanishes.
- spam
- offensive
- disagree
- off topic
LikeYes you are right, but we are not supporting Thesis on this site because it is not compatible with the GPL License.
- spam
- offensive
- disagree
- off topic
LikeIts confused me Bro, but i make some simple plugin that allow you to replace with > ;
<?php
/*
Plugin Name: Filter Comments
Plugin URI: http://cruzenaldo.com/plugin-sederhana-filter-kome...
Description: Plugin sederhana untuk melakukan filterisasi terhadap komentar dan mencegah user menginput tag - tag HTML
Author: Cruz3N
Author URI: http://www.cruzenaldo.com/
Version: 1.0
*/
function my_function ($text) {
$text = str_replace('', '> ;', $text);
return $text;
}
add_filter('comment_text', 'my_function');
?>
You can modification that better... Hope usefull
Download here
http://www.box.net/shared/rgb4lmt5uy
And this is my ugly blog :p
http://cruzenaldo.com/
Best Regard
Cruz3N
- spam
- offensive
- disagree
- off topic
LikeThank you for the useful tuto and easy to follow. I've found another one explaining how to disable HTML but was to hard and badly explained, anyway thanks again and have a great week end :)
- spam
- offensive
- disagree
- off topic
Likethis great and should help to block out loads of spam :) thanks
- spam
- offensive
- disagree
- off topic
LikeThanks for the Great Tips.
Is it affecting commentluv plugin?
- spam
- offensive
- disagree
- off topic
LikeHaven't tried yet, but don't think it should.
- spam
- offensive
- disagree
- off topic
LikeThis will be of great help from those spam bots that try to leave comments with twenty or so links...LOL:) Thanks
- spam
- offensive
- disagree
- off topic
LikeThanks! Didn't know how to do this before.
- spam
- offensive
- disagree
- off topic
Like