How to Remove HTML Tags from WordPress Comments

How to Remove HTML Tags from WordPress Comments

Online by JSC0d3
November 8, 2017 | | | 1372 recognitions

Nowadays, a lot of bloggers choose to remove HTML tag from WordPress websites. However, for beginners and newbies in this field, they may feel confused about the practice. Meanwhile, some webmasters may have realised the importance to do so but lack effective solutions. In order to help people out, our professional and experienced editors carefully explain why to remove HTML tags from WordPress and propose multiple channels to achieve the goal.

Why Remove HTML Tag?

By default, WordPress allows users to use HTML in the comment section. This provides for attractive formatting since they can bold, italicize, and underline text. Unfortunately, it sometimes happens that people end up posting excessive links and other annoying markup in the comment section. If you find your discussions becoming unreadable and want to do away with the ability to render HTML markup in blog discussions, this article is for you.

How to Remove HTML Tags from WordPress Comments

Keep in mind that if it’s only a question of appearance you might be able to get by with changing the CSS styling of the links. But if you’re concerned about link spamming for SEO reasons, then you might be better off disabling links (or other HTML) in general.

Disable “Clickable URLs”

WordPress has a feature by which all URLs pasted in the comment section automatically become “clickable”. This means that even without any kind of specific HTML markup, users will still be able to post HTML links in the discussion area. It consists of a normal line of text, one with the “strong” HTML tag, one with just a regular link pasted in, and the last one with an explicit HTML link.

To start off with, this is what we’re going to disable first. Open up your functions.php file or any other place where you keep your custom PHP code and type in the following:

remove_filter('comment_text', 'make_clickable', 9);

This simple snippet removes the filter called “make_clickable” which parses comments and makes all URLs clickable. If your only concern is not allowing plaintext links to become clickable, your job is done. But what if the person writing the comment explicitly makes it an HTML link? Note that the last line is still clickable. Luckily for us, we have a way to remove even explicitly defined HTML tags from the comment section.

Stripping HTML Tags from the Comments

What we’re going to do is parse all comments through a function that will get rid of HTML tags. We can either choose to go nuclear and clean all tags or be selective. In the example below, I’m going to disable all HTML tags except for, bold text, italicised text, and paragraphs. As before, open up your functions.php file and paste in the following:

if ( ! function_exists( 'js_remove_html' ) ) {
  function js_remove_html($comment) {
     return strip_tags($comment, '<strong><b><em><p>');
add_filter('get_comment_text', 'js_remove_html');

The “get_comment_text” filter allows us to redirect the comments text into a custom function of our own. Inside this, we make use of the “strip_tags” built-in PHP function and pass the comments text variable as the first parameter. The second parameter takes a list of HTML tags that we want to exclude from the stripping process. Note how there are no quotes or commas separating the individual tags from one another.

You can see that this time all the links now render as plaintext and none of them are clickable. At the same time, our “bold” HTML is still being rendered.

Use these two snippets of code to achieve the effect you want. If you’re merely looking to limit WordPress’s ability to generate clickable links, the first piece of code will do the job. If you’d like to strip out all HTML tags with the exception of a few, use both the first as well as the second.

Apart from achieving your goal with WordPress, you can also use WordPress plugins to do so. Coming down to the essence of the problem, it is a matter of spam comments and accordingly, you can use comment plugins to achieve your goal. If you want to be precise, WordPress also offers HTML tag removing plugins.

Wrap up

Since i have removed the comment form allowed tags from this site, the spam has stopped completely.

How about you? Did you update to HTML 5 and have the same problem? Did removing the allowed tags solve the problem?

Please share any tips you have or experience preventing comment spam. Thanks

JSC0d3's Logo
About JSC0d3

JSC0d3 is an entrepreneur, online marketer, and an employee of an IT company. When not building websites, creating content, or helping customers improve their online business, spend time with their wife and two beautiful children. Although he still feels new in WordPress, he enjoys sharing what he has learned with all of you! If you want to get in touch with him, you can do this through this website.

On the same idea

Posted by | February 9, 2019

How fast your website loads can have a direct effect on whether you turn visitors into new customers and fans When your site is delivered to...

Posted by | February 9, 2019

With today’s high-speed internet connections and advances in web development techniques, there is no reason that your website should be...

Posted by | February 9, 2019

WordPress' wp_link_pages() function, used for displaying page links in multi-page posts, lacks one big feature You can display a list of...

Previous PostBackNext Post
8 impressions on “How to Remove HTML Tags from WordPress Comments

Sarah Wayland,

Just recently found you after learning WP the hard way over the last few years. Wish I had found you earlier! This article on 55+ Most Wanted WP… was very helpful.


J&S Code,

We’re glad you enjoyed, thanks so much.

Tomoko Gallo,

Usually I don’t read post on blogs, however I would like to say that this
write-up very compelled me to check out and do it!
Your writing taste has been surprised me. Thanks,
quite great post.

J&S Code,

We’re glad you enjoyed our website, thanks so much.

SEO Tutorials,

I am sure this piece of writing has touched all the internet people, its really really fastidious
paragraph on building up new web site.

J&S Code,

Thanks for your opinion


Neat blog! Is your theme custom made or did you download it from somewhere?
A design like yours with a few simple adjustements would really make my blog jump out.
Please let me know where you got your theme. Appreciate it

J&S Code,

It was custom made by me.

Leave here an impression