I was recently working on a piece of code that needed to do the following:
- Create a special user role
- Allow that user role to insert HTML comments on posts—specifically, we needed support for images and links—directly from the WordPress admin panel
Everything worked perfectly when I tested it using an admin account. The comment included HTML content like images and links and displayed exactly as expected. However, when I switched to the new user role and tried the same thing, WordPress stripped out all the HTML.
The Issue With Adding HTML Comments
After some debugging inside WordPress, I found the issue. It wasn’t a bug—it was actually a default behavior of WordPress. By design, only certain user roles are allowed to add html comments. That’s because WordPress uses capabilities to control what users can do, and the ability to add html comments is tied to the unfiltered_html
capability.
The Solution
If you’re unfamiliar with how WordPress handles user permissions, this guide on roles and capabilities is a great starting point. It explains how capabilities, like unfiltered_html
, control what users can and can’t do—including whether they can add html comments.
So, if you’re building a custom role and want those users to add html comments without WordPress stripping out the tags, you need to ensure the right capabilities are in place. Once that’s done, your custom users will be able to leave rich, formatted comments just like an admin.
Once again, the best documentation for WordPress is the actual source code.
Follow the code and you will see the light 🙂
At WPRiders, we know your website isn’t just a project—it’s part of your business engine ⚙️. That’s why we offer more than just code.
If that sounds like what you need, 👉 Schedule a Free Discovery Call and let’s talk about your next move. We’re always up and ready for a good conversation.
