WordPress: How to remove Image Attachment Pages

There is a mobile optimized version of this page, view AMP Version.

WordPress has an annoying feature where it creates a page for every single attachment in your WordPress system, including images, audio/video, pdf's and etc. I don't see any reason for this functionality as usually attachments are embedded into pages and posts and should never have their own page.

Whats even worse is that by default WordPress will link your attachment to these pages when you are building a post, most of time though people realise this quite early and change the default option by selecting what they require, but these pages still exist and can be discovered by search engines and other bots.

Luckily you can eradicate the feature by creating a template file for it and simply redirecting to your homepage or the attachment source page (although this didnt work for me so I recommend you redirect to the homepage). Follow the steps below:

  1. In your theme directory or your sub-theme directory create (or edit) image.php

  2. In this file delete all content and place the code shown below (choose the one you prefer):

    Redirect to parent post:

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

    Redirect to homepage:

    <?php wp_redirect('/'); ?>

  3. That's it your done!

 

Hope this is useful to someone, tore my hair out for a while until I found this solution!

Author: Dean Williams

I'm a Web Developer, Graphics Designer and Gamer, this is my personal site which provides PHP programming advice, hints and tips