Disable media attachment pages in WordPress

For every uploaded image and other media an attachment page is automatically created in WordPress, which is accessible under its own URL. For your SEO this is not always optimal and often webmasters don’t even have this fact on their radar. In rare cases these attachment pages are really useful for the visitor. We show you how to disable them completely, with and without plugin.
What are WordPress attachment pages?

Every time you upload a file to the WordPress media library, the CMS creates a separate subpage for it. For example, if you integrate an image and link to the attachment page as provided by default, it becomes visible to the public and thus also to search engine crawlers. The added value of these attachment detail pages is usually rather low. Experience shows that they disturb the interaction more than they benefit the visitor.

The design of the attachment pages depends on the WordPress theme used. Mostly it is a similar or the same template as the one used for normal articles. So the content area will just appear with the one image, video or media you uploaded.

In some cases, there may also be problems with existing URLs if the permalink corresponds to an existing page. This is because the filename of the uploaded media is taken for the URL slug, and thus can block the existing deep link of another subpage.

How can I disable attachment pages without a plugin?

A simple code snippet can help. You place this in the functions.php of your active (child) theme:

// Disable attachment pages
function pressengers_redirect_attachment_page() {
  if ( is_attachment() ) {
    global $post;
    if ( $post && $post->post_parent ) {
      wp_redirect( esc_url( get_permalink( $post->post_parent ) ), 301 );
      exit;
    } else {
      wp_redirect( esc_url( home_url( '/' ) ), 301 );
      exit;
    }
  }
}
add_action( 'template_redirect', 'pressengers_redirect_attachment_page' );

This code will 301 forward all attachment pages that are in your WordPress installation and would be created in the future. The target of the redirect is then the actual file, which is called directly. So for example the image, without an HTML page being delivered for it.

If you want to create only certain attachment types or basically go through a corresponding template file, you can also create for example an image.php in the theme folder with the following code:

<?php
global $post;
if ( $post && $post->post_parent ) {
 wp_redirect( esc_url( get_permalink( $post->post_parent ) ), 301 );
 exit;
} else {
 wp_redirect( esc_url( home_url( '/' ) ), 301 );
 exit;
}

Forward attachment pages with plugin

For this small customization we don’t recommend to install an extra plugin. Basically, you should rather be sparing with extensions to your WordPress installation, as you make yourself dependent and more vulnerable. While it’s easy to quickly install a WordPress plugin, we want to be economical with our existing resources.

However, there are plugins that include this feature alongside others. For example, All in One SEO or Yoast SEO. With AIO SEO, you can get to the “Redirect Attachments” option via the settings page under “Media”, where you can select if and where attachment pages should be redirected to.

With Yoast SEO you can find this setting under the menu item “Appearance in search”, behind the tab “Media