WordPress Tip: Remove All HTML Tags In Comments

WordPress Tip: Remove All HTML Tags In Comments

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!


  1. 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?

    1. Hi rilwis, could you paste code as mentioned by you? I would love to have this working.
      Thnx in advance for time and effort.

  2. 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’);

  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.

  4. I happen to enter your blog with the help of Google search. To my sheer luck I got what I was searching for. Thanks.

  5. 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 😆

  6. 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!

Comments are closed.