Removing HTML from WordPress Comments: A Quick Guide
One of the most forgotten parts of managing a WordPress website is how you deal with comments. The comments section is where your visitors share your comments, participate in conversations and ask questions. But if left not moderate or poorly configured, it can also become a place where spam thrives, code injections occur and its layout breaks - all due to a common factor: HTML in the comments.
Normally, WordPress lets users include specific HTML tags in their comments. Although this may seem useful, it can be easily used or lead to unintentional problems. If you want to remove the HTML from WordPress comments, this guide will take you to each step with practical, safe and easy-to-understand methods, without hidden tricks without complicated plugins.
This guide is designed for WordPress -owners, bloggers, developers, developers, and beginners who want to maintain a clean, safe and easy to use comments section.
Why You Should Remove HTML from WordPress Comments
Allowing HTML in comments may seem harmless, but there are several important reasons to reconsider this default setting.
1. Improve Website Security
HTML tags might be misused to add harmful code. Even if WordPress filters some tags, advanced users or bots can find ways around these filters. This opens the door to XSS attacks, where attackers inject scripts into your site via comments. Disabling HTML reduces the chance of this happening.
2. Eliminate Spam Links
Spammers often add clickable links to their comments using <a> tags. This tactic not only looks unprofessional but also harms your site’s credibility and SEO. These links can redirect your visitors to shady or harmful websites.
3. Prevent Layout Issues
If a user posts incorrect or malformed HTML, it can distort your page’s layout. This becomes particularly problematic on mobile devices, where every inch of screen space matters. Stripping HTML ensures that the formatting remains consistent and clean.
4. Reduce the Risk of SEO Penalties
Search engines can penalize websites that contain spammy or suspicious outbound links. If users post such links using HTML in comments, your domain’s reputation might suffer—even if you didn’t post the content yourself.
5. Maintain a Better User Experience
Simple, clean comments are easier to read and moderate. When HTML is allowed, it can lead to confusing formatting, inconsistent font sizes, and other display issues.
How to Remove HTML from WordPress Comments (Step-by-Step)
There are several ways to disable or limit HTML in comments. Whether you're comfortable editing PHP or prefer not to touch code at all, there’s a method for you.
Method 1: Strip All HTML from Displayed Comments
This method ensures that any HTML tags in a comment will not be shown to your site visitors.
How to implement:
- Go to your WordPress admin dashboard.
- Navigate to Appearance > Theme File Editor.
- Locate the
functions.phpfile of your active theme. - Add the following code at the bottom of the file:
function remove_html_from_comments($comment_text) {
return strip_tags($comment_text);
}
add_filter('comment_text', 'remove_html_from_comments');
What it does: This function strips all HTML tags from comments when they are displayed on your website. It doesn’t affect how comments are stored in the database.
Method 2: Sanitize Comments Before Saving Them
To completely remove HTML even before the comment is saved to the database, use the preprocess_comment filter.
function sanitize_comment_before_save($commentdata) {
$commentdata['comment_content'] = strip_tags($commentdata['comment_content']);
return $commentdata;
}
add_filter('preprocess_comment', 'sanitize_comment_before_save');
This method ensures that your database remains free from HTML content in comments, which is beneficial for performance, storage, and security.
Method 3: Allow Only Specific Safe HTML Tags
In some cases, you might want to allow very limited formatting—like bold or italic text—while still preventing other tags.
function allow_limited_html_in_comments($comment_text)
{
return strip_tags($comment_text, '<strong><em>');
}
add_filter('comment_text', 'allow_limited_html_in_comments');
This lets users apply basic formatting without exposing your site to unnecessary risk.
Method 4: Disable Auto-Linking in Comments
WordPress changes plain web addresses into clickable links using the make_clickable filter. You can turn off this option with just one line of code:
remove_filter('comment_text', 'make_clickable', 9);
Disabling auto-linking is another effective step toward preventing spam and unwanted outbound links.
Method 5: Remove Website Field from Comment Form
Many spammers target the “Website” field in comment forms to place their links. Removing this field can discourage link-hungry bots and reduce low-quality comments.
add_filter('comment_form_field_url', '__return_false');
This small tweak can make a big difference in improving the quality of your comment section.
Should You Use a Plugin?
If you’re not comfortable editing PHP files, you can use a plugin that allows custom snippets or disables comment features. Look for plugins in the WordPress Plugin Directory that offer comment moderation or custom code integration.
While plugins are convenient, using a small piece of code is often the cleaner and faster solution—especially for something as simple as stripping HTML tags.
Frequently Asked Questions (FAQs)
Q: Can visitors post HTML in WordPress comments by default?
Yes, WordPress allows some HTML tags such as <a>, <strong>, and <em> in comments by default.
Q: Will removing HTML affect old comments already posted?
No. These changes apply to new comments or how comments are displayed. If you want to clean existing comments, you’ll need to run a database query or use a plugin.
Q: Do I need to install a plugin to remove HTML from comments?
No. This can be done easily using PHP snippets in your theme’s functions.php file. However, plugins are available for users who prefer not to touch code.
Q: Does removing HTML affect SEO?
Yes, in a positive way. Removing HTML prevents spam links and malicious code from appearing in your comments, which protects your site’s search engine credibility.
Q: Can I allow some HTML but block others?
Yes. Use strip_tags() and specify which HTML tags should be allowed.
Final Thoughts
Cleaning up your WordPress comment section by removing HTML is a smart, effective step to create a better user experience, protect your website, and reduce spam. With just a few lines of code—or the help of a plugin—you can strip away unnecessary formatting, eliminate security risks, and keep your site’s layout intact.
Here’s a quick recap:
- Remove all HTML tags using
strip_tags(). - Sanitize comments before they are saved to the database.
- Allow only safe formatting tags like
<strong>or<em>if needed. - Disable auto-linking of URLs in comments.
- Remove the website field from the comment form to discourage spam.
These small changes can lead to big improvements in your site’s security, cleanliness, and professionalism.
By applying these strategies, you ensure your WordPress comment section is safe, simple, and focused on real conversation—the way it should be.

Comments
Post a Comment