Skip to content

Early-bird lifetime licence: $199 once, for the first 200 buyers only. See the offer

Redirect attachment pages in WordPress without a plugin

Stop WordPress showing a thin page for every upload with one line of code. The Snipfire snippet sends visitors to the file; Attachment Pages Redirect sends them to the post.

What Attachment Pages Redirect does

Attachment Pages Redirect sends visitors from an image's attachment page to the post it was uploaded to, or to the home page if there isn't one. The plugin on WordPress.org

What the snippet does the same

  • Visitors and search engines no longer see the thin attachment page for each upload.
  • There are no settings in either.

What's different

  • The snippet sends visitors to the file itself, using the setting WordPress 6.4 and later uses on new sites. The plugin sends them to the post the file was uploaded to.
  • The plugin sends files that aren't attached to a post to the home page, and shows a 404 page when the post is in the bin. The snippet sends every attachment page to its file.

When to keep the plugin Keep the plugin if you would rather send visitors to the post an image belongs to than to the image file.

The snippet: Send attachment pages to the file

Each uploaded image has its own thin page (…/image-name/). This sends visitors straight to the file instead, as new WordPress sites do.

PHP
<?php

// The same setting WordPress 6.4+ uses on new sites.
add_filter( 'pre_option_wp_attachment_pages_enabled', static fn() => '0' );

Notes, where it runs and what it was tested with

Switching from the plugin

  1. Install the snippet in Snipfire (it arrives switched off).
  2. Deactivate Attachment Pages Redirect.
  3. Switch the snippet on. Snipfire checks the code and test-loads your site first.
  4. Check the page it affects, then delete the plugin.

Move your snippets over this afternoon.

Importing changes nothing until you switch over, and your old shortcodes keep working.