How to Disable Image Attachment Pages in WordPress

Uploaded WordPress files including images, videos, and audios are stored in the exact same way as pages and posts, called “attachment pages”. They have titles, authors, publication dates, URLs, and so on. Their links are accessible via search or by direct links.

In fact, these pages don’t bring much value but poor user experience. They sometimes affect your site SEO and pull down the page’s rankings. That’s why most site owners plan to redirect attachment pages to the original page URLs.

In this post, we’ll touch on why you should remove the image links. We’ll also discuss some key methods to disable your attachment pages. Let’s get started!

Why Removing Attachment Pages in WordPress

As mentioned, once you add a file to your WordPress media library, it’ll automatically create a URL for that file. Although you never check the “Attachment Pages” option under “Link To” in the Attachment Display Settings, it somehow references your attachment page through search or the direct link.

 pda-attachment-url

The most common and important reason for disabling attachment pages comes to SEO issues. If your images are indexed on Google, they might rank somewhere and gain traffic to the attachment pages instead of the main page or posts. As a result, you could lose an opportunity to engage visitors to read your content.

The attachment pages badly affect the user experience too. Since users see the files without any content, they’ll leave the page right away which increases bounce rates on your site.

 

An image URL consists of the page’s URL as the permalink. For example, https://mysite.com/how-to-disable-image-attachment/image-1 or https://mysite.com/how-to-disable-image-attachment/image-2. Google might suppose that you provide duplicate content. Consequently, just one of the images will be indexed, not the main page or the post’s link.

Another reason for removing attachment pages’ URLs is password protected content. Even though your post is locked with a password, the image is still available on the Internet. If it’s shared widely on social media and someone clicks on the link, they can see the file without having the correct password.

It’s possible for you to prevent visitors from viewing these attachment pages by redirecting them back to the primary page or the homepage. You have multiple ways to remove images’ links, from coding methods to using plugins.

How to Disable Image Attachment Pages Manually

#1 Redirect via the image.php file

This solution requires you to have an image.php file in your child theme folder. Some themes provide this file by default so you need to check it before creating one. After that, place the code below as the first lines of your files:

<?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;
}

#2 Redirect through the custom function

You’re enabled to bring visitors back to the main post or the homepage via the functions.php file. You should:

  1. Go to Theme Editor under Appearance in your WordPress admin dashboard
    appearance-theme-editor
  2. Select functions.php in the Theme Files menu at the right side
    edit-theme-file 2
  3. Place the following code in the file
function myprefix_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', 'myprefix_redirect_attachment_page' );
How to Remove Attachment URL Using Yoast SEO

How to Remove Attachment URL Using Yoast SEO

In case you’re new to WordPress and code snippets, this method is for you. Since Yoas is recently the most common plugin for WordPress SEO, many site owners can apply the solution with a few simple clicks. Not only does Yoast manage site appearance on search engines, but it also handles media file redirection.

Once installed, the plugin will be added right to the WordPress navigation menu. To redirect the attachment page, you need to:

  1. Head to Search Appearance under the Yoast SEO symbol
    pda-yoast-search-appearance
  2. Open the Media tab and choose the Yes button under Media & attachment URLs
    pda-media-attachment-urls

Disable Attachment URLs with Attachment Pages Redirect Plugin

Trusted by over 20 thousand users, Attachment Pages Redirect works as the best tool to lead visitors from the attachment page to the parent post. If the parent post was deleted, your visitors will be sent to the homepage or a 404 not found page.

After installing the plugin, it starts redirecting users immediately. This means that there are no settings or configurations required. All you need to do is:

  1. Open Plugins in the WordPress dashboard and click Add New
  2. Type “attachment pages redirect” in the search box
  3. Install and activate the plugin
    pda-add-attachment-redirect-plugin

How to Disable Image Downloads

In some cases, not only do you want to remove image attachment URLs but you’d also like to prevent users from downloading these files.

To achieve that, Prevent Direct Access Gold should be your way to go. Once protected, the file URLs won’t be available to all users but the allowed ones only. Unauthorized users are redirected to 404 not found pages. At the same time, the plugin blocks search engines from indexing your files. As a result, visitors aren’t able to look for your protected file on search engine results.

Follow the guide below to get started with the plugin:

  1. Install and activate Prevent Direct Access Gold plugin
  2. Go to Media Library in your WordPress dashboard
  3. Click Configure file protection of any file you intend to secure
  4. Hit “Protect this file”

Now you can see a file management popup displaying all elements of a protected file, including download links, its status, download limit, download expiry, and the created time.

It’s possible for you to protect and have your download links expire after certain days or a number of clicks by clicking on the Edit icon.

It’s Time to Remove Your WordPress Image Links

Image attachment pages create a poor user experience and affect your site SEO negatively. Google might index these URLs instead of the parent page and bring traffic to these nonsense attachment pages.

There are several methods you can apply to disable file attachments in WordPress, from coding to third-party plugin solutions. You’re able to add code snippets to the image.php file or the functions.php file. Either Yoast SEO or Attachment Pages Redirect proves simple and useful tools to redirect image links.

Out of the box, you’re allowed to protect any of your files by redirecting the original URLs to 404 pages using Prevent Direct Access Gold.

Still have a question about how to disable image attachment pages on your WordPress site? Let us know in the comment section below.