Disable attachment pages
Turn off WordPress attachment pages so their URLs redirect to the media file, on sites created before WordPress 6.4.
// Turn off attachment pages: their URLs redirect to the file itself.
add_filter( 'pre_option_wp_attachment_pages_enabled', '__return_zero' );
What it does
WordPress creates a separate page for every image and file you upload. These thin pages can get indexed by search engines and add little value.
WordPress 6.4 added a setting that turns them off, but only new sites have it off, and there's no screen to change it. This snippet turns attachment pages off without touching your database. Their URLs redirect to the file itself.
Good to know
- Needs WordPress 6.4 or later.
- Remove the snippet to bring attachment pages back. Nothing else is changed.