WordPress Tip: Remove All HTML Tags In Comments

html WordPress Tip: Remove All HTML Tags In Comments

By default, WordPress allows commentators to use some HTML tags in comments for links, quote or format text. But in many blogs, these tags are rarely used, or even they’re used for spam links. In that case, we might want to let the commentators use only plain text in comments without any HTML tags.

To remove all HTML tags in comments, paste the following code into functions.php file:

add_filter('comment_text', 'wp_filter_nohtml_kses');
add_filter('comment_text_rss', 'wp_filter_nohtml_kses');
add_filter('comment_excerpt', 'wp_filter_nohtml_kses');

Here we apply WordPress built-in function wp_filter_nohtml_kses to some filters, used to display comments. The function wp_filter_nohtml_kses will strip all HTML tags in some string, in this case – comment.

Using wp_filter_nohtml_kses, we don’t have to care about writing handy code for checking HTML tags using regular expressions (usually, it’s a hard job). Because it’s built-in WordPress core, it’s really safe and fast!

10 Comments

  1. rilwis, I added this code to my functions.php file, however visitors can still post strong tags. Is there a way to eliminate all types of HTML? thanks!

    Reply
  2. Mình tìm cách làm điều này mãi mà không được, may là search google ra bài viết này của bạn. Thanks chủ web nhè. Mình sẽ ghé lại blog bạn thường xuyên hơn để khám phám những cái hay. Mà còn cái nữa là cái theme đơn giản mà quá đẹp, nếu bạn có nhã ý share mình với nha :lol:

    Reply
  3. It’s amazing to go to see this web page and reading the views of all mates on the topic of this post, while I am also eager of getting know-how.

    Reply
  4. If it’s a new blog with no comments and you want to do this for all posts try

    add_filter( ‘pre_comment_content’, ‘wp_filter_nohtml_kses’);

    Reply
  5. This is a very nice code snippet, is it possible to disable this for users only, so admin/authors could still use the html function?

    Reply

Leave a Reply