How to Remove HTML Tags from WordPress Comments

How to Remove HTML Tags from WordPress Comments

Online by J&S Code
8 November 2017 | PHP WP snippets, code & hacks | 1272 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:

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:

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

About The Author

J&S Code

Sorin C 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.

Opinions

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.

Thanks!
Sarah

    We’re glad you enjoyed, thanks so much.

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.

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

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

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

Put here your thoughts

Your email address will not be published. Please complete all fields * correctly You may use these HTML tags: bold text, italicised text, and paragraphs.

On the same idea

Securing WordPress AJAX Forms Using Nonce
Online by stevenmedia | 7 August 2018

When creating a WordPress theme or plugin, AJAX is often used in order to enhance the user experience. In order to ensure security and protect your site against several types...

AJAX for WordPress
Online by stevenmedia | 7 August 2018

This tutorial is just a simple guide to understand the basic about using AJAX in WordPress. (more…)

Customizing the WordPress Query
Online by stevenmedia | 6 August 2018

One of the most powerful features of WordPress is the WP Query. It is what determines what content is displayed on what page. And often you’ll want to modify this...