# Close Comments on Media Attachments

> Prevents comments on attachment pages, a frequent target for spam bots.

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

## Code

```php
add_filter( 'comments_open', function ( $open, $post_id ) {
    $post = get_post( $post_id );

    return ( $post && 'attachment' === $post->post_type ) ? false : $open;
}, 10, 2 );
```

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Close Comments on Media Attachments" 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/
