Resources     Blog  

  1. Home
  2. WordPress Tutorials
  3. How to Set WordPress to Disable Attachment Pages for Media

How to Set WordPress to Disable Attachment Pages for Media

When you upload a file such as an image, WordPress creates a URL for that file. This URL is accessible through search or by direct link. However, not everyone wants this attachment page to appear on the site.

In this tutorial, I’ll show you how to make WordPress disable those attachment pages for media. In fact, I’ll show you three separate ways to do this.

What are Attachment URLs?

Attachment URLs act and appear much like any other post or page in WordPress. The biggest difference is the file is the only thing showing. They will use a custom permalink in WordPress much like how posts and pages.

Which means a visitor can click on a link from Google and see a page displaying just a single image with no other content.

Well, except for your sidebars and footer.

For an unsuspecting user, it could appear the site is incomplete or lacking the information he or she is looking for. In other words, you could lose an opportunity to engage this individual.

Method 1: Using Yoast SEO

The first method to disable media attachment URLs is by using Yoast SEO. This plugin is full of tools to manage how your site appears in engines like Google. One of which is how it handles a redirect for media URLs.

If you don’t have it, I suggest installing Yoast SEO. It’s an incredible tool to help you score better in search results as well as creating content for visitors.

For myself, it’s usually one of the first plugins I install on any site I help develop.

From your WordPress dashboard, go to SEO and click, “Search Appearance.”

Yoast Search Appearance

Click the “Media” tab from the top.

Yoast Media

Click the “Yes” button under Media & attachment URLs. This will set media URLs to redirect to the file itself and not create a special page on your website.

Yoast Attachment

This will remove the SEO options in Yoast for file attachment pages. Click the “Save changes” button.

Save Attachment Changes

Method 2: Using Attachment Pages Redirect

The Attachment Pages Redirect plugin is a fine tool to use. It will create redirects according to how a user lands on an attachment page.

For instance, the plugin will create a 302 redirect to the home page if the original post is in the trash but users still access the website through the image or file URL. This means there is no 404 file not found error shown to the visitor.

Install and activate “Attachment Pages Redirect.

Attachment Pages Redirect

Once activated, the plugin will automatically begin redirecting users. There are no settings or options to manage in this plugin.

It’s probably one of the easiest tools to make WordPress disable attachment pages for media. You essentially set it and forget it.

Method 3: Adding a Code Snippet

Another way to remove media URLs from WordPress is by adding a code snippet to the image.php file of your theme.

You can do this a number of ways. Some prefer to use FTP programs such as FileZilla to edit website files. For this tutorial, I’m going to just use the File Manager tool in cPanel.

Of course, it’s always best to make adjustments like this in a child theme. That way if the theme is updated, you don’t run the risk of having the snippet removed.

Access the wp-content folder of your site from the root directory.

WP Content Folder

Open the themes folder.

Open Themes

Access your theme’s folder from the list. In this tutorial, I’m using the “twentysixteen” theme. If you’re not sure which you’re using, you can always click the “Appearance” option in WordPress to see which you have activated.

Access Theme

Edit the image.php file of the theme.

Edit Image PHP File

Add this code to the top of the file:

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

Add Code Snippet

Once you save the changes, WordPress will redirect the attachment URL to the original post.

Clean Up WordPress and Remove Media URLs

These are only a few methods to set WordPress to disable attachment pages for media. With various plugins, you have access to a myriad of options. In many instances, plugins which focus on SEO support have the option for redirecting attachment URLs.

It’s all a matter of finding the best method which works for you.

How often do you have visitors access attachment URLs without exploring the site further? Do you prefer coding or plugins when it comes to making adjustments such as this?

Author: Michael Brockbank

Michael has been in the tech industry in some form or another since the late 1990s. He’s built a variety of websites using HTML, Joomla and WordPress. Starting his freelance writing career in 2012, he’s completed thousands of projects world-wide covering a wide scope of topics. Michael is currently the Content Marketing Team Lead. Today, he manages a team of writers to create tutorials, blog posts and support content for customers. Outside of GreenGeeks, he manages a YouTube channel that helps others learn how to become freelance writers, bloggers and overall professionals.

Was this article helpful?

Related Articles


  1. Excellent. Created an image.php file with your single line of code above and stopped a “comments to attachment page” that was wearing me out. Thank you.

  2. Thanks for the help, I sort of use method 3, though I do call my 404 template instead.

    One issue with this:
    If I have an image called test.jpg (so the title is “test”), it will still reserve the permalink path /test/ for this image. So If I create a page called “test”, it will receive the path /test-2/ as permalink.
    Any idea how I can prevent WordPress from reserving the permalink path from the image without possibly renaming all image titles to something weird (like appending random numbers)?

  3. I used method 1. I already have the plugin installed and it was the easiest way to solve it.

    Thanks for sharing. Success!

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.