# Strip HTML From Comments

> Removes all HTML tags from comments submitted by users who are not allowed to post unfiltered HTML, so links and formatting cannot be injected.

- Type: PHP
- Runs: Run everywhere
- Category: [Comments](https://wpide.com/library/category/comments/)
- Tags: comments, html, spam
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/strip-html-from-comments/

## Code

```php
add_filter( 'pre_comment_content', function ( $content ) {
    if ( current_user_can( 'unfiltered_html' ) ) {
        return $content;
    }

    return wp_strip_all_tags( $content );
}, 9 );
```

## Notes

URLs typed as plain text are still turned into links by WordPress when displayed.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Strip HTML From Comments" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
