How to Disable Image Attachment Pages in WordPress (2 Methods)

After 15 years as a webmaster, I‘ve seen hundreds of WordPress sites suffer from image attachment page bloat.

These auto-generated pages clutter up your content and can seriously drag down site performance.

In this guide, I‘m going to show you how to disable image attachment pages the right way.

By following these two simple methods, you can eliminate unnecessary attachment pages for good.

Why Image Attachment Pages Are Bad for SEO

Before we dive in, let me quickly explain why you should disable attachment pages for SEO:

  • They create duplicate pages targeting the same image, which dilutes your ranking power. Google sees these as extremely similar.

  • Crawling all those extra pages can slow down indexing. I‘ve seen sites grind to a crawl once they have 100k+ attachments.

  • Attachment pages lack context for search engines. Even with good alt text, it‘s harder for Google to understand the topic.

  • Having media hosted across multiple URLs makes it tough for Google to aggregate signals. The attachment URL competes with the parent URL.

  • Visitors who reach an attachment page leave quicker. Without context, they‘ll hit the back button instead of continuing to explore your content.

In other words: attachment pages are bad for bots and users alike.

Let‘s look at a real example.

Say you upload this image to a blog post:

SEO attachament page example

By default, WordPress also generates an attachment page:

https://www.example.com/wp-content/uploads/seo-attachment-page.png

Now Google has to index and process two separate URLs for the same image.

The attachment URL only contains the image with no context:

Attachment only image example

While the post URL shows the image in context:

Image in context example

Clearly, the post is where you want the link equity and engagement to be directed.

But attachment pages siphon away traffic. In this example, the attachment page got 34 visits vs only 4 on the post according to Google Analytics.

That‘s why we need to disable these unnecessary pages.

Next I‘ll show you how.

Method #1: Disable With a Plugin (The Easy Way)

The simplest way to remove attachment pages is using a WordPress SEO plugin.

I recommend Yoast SEO as the best option:

  • Used on over 5 million WordPress sites
  • Very customizable settings
  • Active development and support

Here‘s how to configure Yoast SEO to disable attachments:

  1. Install and activate the Yoast SEO plugin.
  2. In the dashboard, go to SEO → Search Appearance.
  3. Click the Media tab.
  4. Under "Redirect attachments to parent", select the Enabled box.
  5. Click Save Changes.

That‘s it! Yoast will now redirect all image attachment pages to the parent post/page URL.

Yoast SEO disable attachment pages

With this single setting, Yoast prevents duplicate pages from being indexed.

I recommend installing Yoast SEO anyway for all the other great SEO features it offers. Disabling attachments is just one small part.

The benefit of a plugin is you can simply toggle the setting on/off anytime. No coding required.

However, plugins do come with some downsides:

  • Dependent on third-party updates and support. If the plugin breaks, attachments could comeback.
  • Often resource-intensive. Extra plugins can slow down your site.
  • Not as transparent as code. Harder to customize and troubleshoot conflicts.

For these reasons, I suggest a manual code approach for some sites…

Method #2: Disable With Code (The Technical Way)

If you‘d prefer not to use a plugin, you can disable attachments with just a few lines of code:

function redirect_attachments() {
  global $post;
  if( is_attachment() ) {
    wp_redirect( get_permalink( $post->post_parent ), 301 ); 
    exit;
  }  
}

add_action( ‘template_redirect‘, ‘redirect_attachments‘ );

Here‘s what this snippet does:

  • is_attachment() checks if the current page is an attachment.
  • get_permalink() finds the parent post/page URL.
  • wp_redirect() redirects with a 301 status.
  • template_redirect hook runs the code early in page load.

To implement this, I recommend using a plugin like Code Snippets:

  1. Install and activate Code Snippets.
  2. Go to Snippets → Add New.
  3. Paste the redirect code into the editor.
  4. Give your snippet a name like "Attachment Redirect".
  5. Click Add Snippet to save.
  6. Under Settings → Code Snippets, activate your snippet.

Now all visits to attachment pages will be redirected to the parent page or home page.

The benefit of a code snippet is you have full control and transparency. There‘s no plugin dependency.

However, there are some downsides to consider:

  • Requires comfort editing code. Non-developers may struggle with tweaking.
  • No built-in settings panel. You‘ll need to manage the snippet directly.
  • Potential for conflicts with themes/plugins. More testing required.
  • Changes made directly to files would override and break the snippet.

My recommendation:

Use the plugin approach for most sites. It‘s easier and less risky.

Consider code snippets for sites where performance and transparency are critical.

Either way, you can‘t go wrong getting rid of attachment pages!

Before You Go: Quick SEO Wins

Now that you‘ve disabled your image attachment pages, here are a few other "quick wins" for improving image SEO:

  • Optimize image file names – Use target keyword plus dashes instead of random numbers/letters. Helps Google understand the topic.

  • Write alt text for every image – Accurate descriptions help search bots associate images with surrounding content.

  • Resize large images – Shrink files >1MB to improve page speed and web vitals scores.

  • Lazy load offscreen images – Delay loading below-the-fold images until the user scrolls down. Faster initial load.

  • Compress images – Use a plugin like ShortPixel to crunch images down to the smallest file size possible without impacting quality.

  • Add width/height attributes – These help prevent layout shift as images load. Improves CLS scores.

Follow these tips in addition to disabling attachments for maximum image SEO impact.

I hope this guide gave you a comprehensive look at removing frustrating attachment pages from your WordPress site. Let me know if you have any other questions!

Written by Jason Striegel

C/C++, Java, Python, Linux developer for 18 years, A-Tech enthusiast love to share some useful tech hacks.