Quantcast
Viewing all articles
Browse latest Browse all 33

How to Disable HTML in WordPress Comments!

Image may be NSFW.
Clik here to view.
disable HTML in comments
HTML in WordPress comments can be a good things, but many times people abuse it, for example by inserting links. 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. Just paste the code below into your functions.php file. If you prefer to use a plugin with the same functionality, you can grab one here.

// 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( "'", '&apos;', $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( '&apos;', "'", $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);

 

Thanks to Peter’s useful crap for this nice code!

If you enjoyed this article, please consider sharing it!!

The post How to Disable HTML in WordPress Comments! appeared first on WordPress Experts.


Viewing all articles
Browse latest Browse all 33

Trending Articles