• Home
  • About
  • Contact
Stack Diary - Helpful Advice for Web Developers
  • Home
  • About
  • Contact
Stack Diary - Helpful Advice for Web Developers
Stack Diary - Helpful Advice for Web Developers
WordPress

How to Disable Image Attachment Pages in WordPress

Alex Ivanovs
June 23, 2022
How to Disable Image Attachment Pages in WordPress
Summary » One of the “features” of WordPress is that it creates a new Attachment URL for every media file that you upload. In other words, uploading an image to your site is the same as publishing a blog post. However, you can quickly run into problems by having search engines index your attachment pages – creating hundreds if not thousands thin content pages that provide no value. So, in this article – we will learn how to fix it and disable attachment pages with a plugin or alternatively through a manual method.

Table of Contents
  • Should you disable direct links to attachment pages?
  • #1: Disabling Image Attachment Pages with a Plugin
  • #2: Redirect Image Pages Without a Plugin

There are practically no scenarios in which you would want your attachment page URLs to be kept available to the general public. This is particularly true for having those pages included in your sitemap.

Because if you do – search engines will crawl and index those pages, despite them having no content (other than the image), and in most cases; the pages will have janky titles that are generated from the image name itself.

Should you disable direct links to attachment pages?

So, one of the things that WordPress does when you upload new files to your Media Library is that it creates a new Permalink URL for those pages. This includes any images, video files, documents, and anything else you upload to your Media Library.

Let’s take for example my collection of WordPress tech blog themes.

If I go back to that post and open it in my WordPress editor – I can click on any one of the included images and open it through Media Library. And this is what I will see:

attachment pages example

As you can see, WordPress has created a separate permalink structure (page) for an individual image included in the post. And, unless I had disabled linking to attachment pages – this image could be accessed as a page by both my readers but also search engines.

Now, imagine having 15 or so images per post, with a total of 100 posts on your blog. That would be well over 1,000+ attachment pages that provide no real value.

So, how to go about fixing this and disable attachment pages for individual files?

#1: Disabling Image Attachment Pages with a Plugin

One of the quickest ways to deal with the issue of attachment pages is to disable (redirect) them in the settings of your favorite SEO plugin. Mine is Yoast SEO, but I know other SEO plugins such as All In One SEO and others provide this feature, too.

If you go to Admin Dashboard -> Yoast SEO -> Search Appearance -> Media – you should see a page that looks like this:

Here you’ll want to select Yes for Redirect attachment URLs to the attachment itself? and that should do it. This is also an approach that I have highlighted in my How to fix 404 Error Pages in WordPress article.

If for whatever reason you’re not using one of the mentioned SEO plugins – you can try using this Disable Media Pages plugin.

Disable Media Pages

Download

Completely remove "attachment" pages for WordPress media. Improve SEO and prevent conflicts between page and…

By Johannes Siipola

5.0 rating based on 7 ratings
(7)
Last Updated: 1 month ago
900+ Active Installs
Compatible with WordPress 5.0

This plugin provides the same exact functionality for disabling media attachment pages.

#2: Redirect Image Pages Without a Plugin

An alternative method for disabling/redirecting image attachment pages is by using a tiny code snippet that you can add to the image.php file in your theme’s root folder.

If the file doesn’t exist, you can create/upload it yourself.

As for the snippet – we will be using the wp_redirect() function.

<?php wp_redirect(get_permalink($post->post_parent)); ?>

First, wp_redirect() will fetch the permalink of the image, then grab the post-id and finally redirect to the parent page whenever someone tries to access the attachment page.

And that about sums it up. With just these 3 outlined methods – it should be plenty to help you disable image attachment page links without hurting your SEO or website structure.

Share this post:

Share on Twitter Share on Facebook Share on LinkedIn Share on Email
Written by

Alex Ivanovs

I've been working on Web projects since 2005 with a primary focus on web development and design. After spending many years on a self-taught path, I discovered a natural passion for writing and have been keeping up appearances ever since. Outside of publishing and tech in general, I love to travel the world and explore how different cultures experience their day-to-day life.
⟵

15 Best WordPress Food & Recipe Themes in 2022

⟶

5 Best Push Notification Plugins for WordPress

Related posts:

  1. How to Fix ‘404 Error – Page Not Found’ in WordPress
  2. 7 Best WordPress Website Builders in 2022
  3. 15 Best Tech Blog WordPress Themes in 2022
Privacy Policy.
STACK · DIARY © 2022
  • Home
  • About
  • Write for Us
  • Disclosure
  • Contact
Stack Diary - Helpful Advice for Web Developers
  • Home
  • About
  • Contact
Categories
  • Code
  • Digital Marketing
  • Tech
  • Web Design
  • Web Development
  • WordPress