How to Remove HTML Tags from WordPress Comments
Posted on Updated onBy default, WordPress allows users to use HTML in the comment section. It 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 markups 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.
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.” It means that even without any specific HTML markup, users will still be able to post HTML links in the discussion area. To illustrate this and other HTML tags, I’ve created a sample comment below.
As you can see, it consists of a standard 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. When approved as a comment, it will look like this:
Note that the second last line – the one with just the URL itself is also clickable. To start 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. When you save your changes, the same comment above should be rendered like this:
As you can see, the second last line is now plaintext instead of an HTML link. If your only concern is not allowing plaintext links to become clickable, your job is done. But what if the person writing the comment 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, italicized text, and paragraphs. As before, open up your functions.php file and paste in the following:
function remove_html($comment) { return strip_tags($comment, '<strong><b><em><p>'); } add_filter('get_comment_text', '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 no quotes or commas are separating the individual tags from one another. With the above code in place, the comment I wrote initially now looks like this:
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 except for a few, use both the first as well as the second.
You are my new hero! Thank you so much, this is just what I was looking for!
Thank you for this utterly simplified guide on disabling HTML from WordPress comments. It works like a charm!
If you are still responding to comments here, I was curious whether it might be possible to disable HTML only for non-admin users? I would like to retain the ability to point readers to useful material elsewhere on the site.
Thank again,
Kind regards
Just use:
if(!is_super_admin()) add_filter(‘get_comment_text’, ‘remove_html’);
I use that last code for me put links but with no success. Any ideas?
What do you mean by “that last code”? The code from the tutorial or from my last comment?