Disable HTML in WordPress Comments

01
Mar

This code snippet will demonstrate how to disable displaying of HTML code within your WordPress comments.

Disable HTML in WordPress Comments

Disable HTML in WordPress Comments

How many times have you had your site visitors post spam links and links to inappropriate sites within your WordPress comment system? Some users tend to think that by adding their website URL in WordPress sites that it is going to increase their SEO for their site. They pack as many HTML URL’s and links into a single comment as they can.

This can be quite frustrating. I see it everyday. Even here on the WPInsite website. Well luckily for us WordPress users we can fight back against this comment and post spam by writing a simple function that will disable HTML in WordPress comments.

Implement the function to disable HTML in WordPress comments

To implement the code to disable HTML in WordPress comments we first need to navigate to our theme directory.
Navigate to wp-content -> themes -> your-curent-theme and open the functions.php file.

Copy the below functions and add it to your current active themes functions.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// This function occurs when the comment is posted
function custom_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 function occurs before a comment is displayed
function custom_comment_display($comment_to_display) {
 
// Put the single quotes back in
$comment_to_display = str_replace( ''', "'", $comment_to_display );
 
return $comment_to_display;
}
 
add_filter('preprocess_comment', 'custom_comment_post', '', 1);
add_filter('comment_text', 'custom_comment_display', '', 1);
add_filter('comment_text_rss', 'custom_comment_display', '', 1);
add_filter('comment_excerpt', 'custom_comment_display', '', 1);
// This function occurs when the comment is posted
function custom_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 function occurs before a comment is displayed
function custom_comment_display($comment_to_display) {

// Put the single quotes back in
$comment_to_display = str_replace( ''', "'", $comment_to_display );

return $comment_to_display;
}

add_filter('preprocess_comment', 'custom_comment_post', '', 1);
add_filter('comment_text', 'custom_comment_display', '', 1);
add_filter('comment_text_rss', 'custom_comment_display', '', 1);
add_filter('comment_excerpt', 'custom_comment_display', '', 1);

Be sure to check out our other great WordPress Code Snippets and WordPress Articles.

Enjoy this article? If so, we would love to hear your thoughts in the comments below

 

Related Blog Posts

Leave a Reply