Stop comments on image and file attachment pages, a common target for spam bots.
add_filter( 'comments_open', function ( bool $open, int $post_id ): bool {
return 'attachment' === get_post_type( $post_id ) ? false : $open;
}, 10, 2 );
add_filter( 'pings_open', function ( bool $open, int $post_id ): bool {
return 'attachment' === get_post_type( $post_id ) ? false : $open;
}, 10, 2 );
Always add PHP to a staging site first, or have FTP or file manager access ready, so you can undo it if something goes wrong.
Every uploaded file can have its own attachment page, and by default those pages accept comments. Real visitors rarely comment on them, but spam bots find them easily. This snippet closes comments and pingbacks on attachments only.
WordPress asks comments_open and pings_open whether a post accepts comments and pingbacks. The snippet answers no for attachments and leaves every other post type unchanged.
Open an attachment page logged out. There should be no comment form.