Media Attachment Pages. Who needs ’em, right?

Attachment Page sampleOne of the oddities of WordPress is that just about everything is a post. Posts are posts. Pages are posts. The menu is a type of post. You can create any number of Custom Post Type (I’ve made CPTs for people, recipes, rental items, sponsors, clients, &c). And one that can drive you a bit batty: Media is stored as a post type. This causes two problems. The first is that you might miss that when you add media to your content, there’s a dropdown box labeled Link To that may be set to Attachment Page. Normally you’d want to choose Media File because that’s how you’d pop the image up in a lightbox. Honestly, over the hundreds of projects I’ve done since 2008, I’ve never had a use for a Media Attachment Page.(1)Okay, yes, you can build a custom Media Attachment page template and doll it all up to look like the rest of your site, though I’ve yet to see or hear of one. But hey, you do you. I’d love to see how it turns out. If you’ve never seen one, go ahead and click on the image at right. For the first time ever, I’ve purposely chosen that setting. Maybe you’ll see it as useless as I do. Go ahead, I’ll be right here when you get back– and you’ll have to click your browser’s back button to continue reading this post. While you’re in there, click on the tiny image again so you can see it blow up in a Lightbox and actually read it.

Now that you’ve seen the Attachment Page with the giant headline that says, helpfully, Attachment Page,(2)Because that’s the title I gave it in the Media Library. You can change it to say whatever you like. Since it’s a post, it acts like one. Change the title, let people comment on it, &c I can move on to the real reason I bring this up: Google indexes those pages. It came up in a discussion on Facebook today – a colleague has added organization members to a site she’s building and found that a search turns up not only that person’s bio but also their photo as an attachment page. There are two ways to fix this.

Yoast It

Redirect AttachmentsThe simpler way, if you’re already using Yoast SEO plugin (and you should, it’s excellent), is to flip a switch that redirects to URLs heading to those attachments so they go to the page where you’ve embedded the media. So, for instance, if a search somehow turns up the image above, clicking on that result will bring you to this post rather than that kind of generic, mostly useless attachment page. Slick.

If you’re not seeing those settings, chances are you haven’t turned on Yoast’s Advanced Settings page.

Code It

If you’re not using Yoast, you’re trying to keep your plugins to a minimum, or you simply prefer the coder way of doing this, there’s a simple snippet of code you can use. Create a PHP file in your child theme folder, call it image.php, pop in this little bit of code, and save the file:

…but you’re better off using Yoast because it’s a handy plugin and and an easy way to follow good SEO practices.

Two Caveats

If you’re using the coding example and you change themes, make sure you create an image.php file in your new child theme. Conversely, if you’re using the Yoast method and decide to stop using that plugin, create that image.php file in your child theme or find another plugin that will handle the redirect for you.

Bonus tip!

While you’re checking things out in Yoast, you might as well head over to XML Sitemaps -> Post Types tab and make sure that you have Media (attachment) set to Not in sitemap. If you’re running Beaver Builder or something similar you’ll also want to exclude that post type from the sitemap as well. Then go to XML Sitemaps -> Taxonomies and do the same for things like Format (post_format) and, in the case of Beaver Builder, Categories (fl-builder-template-category). If you’ve built a fairly complex site there will likely be other content that you don’t necessarily want or need to have showing up in your search results.

Asides   [ + ]

1. Okay, yes, you can build a custom Media Attachment page template and doll it all up to look like the rest of your site, though I’ve yet to see or hear of one. But hey, you do you. I’d love to see how it turns out.
2. Because that’s the title I gave it in the Media Library. You can change it to say whatever you like. Since it’s a post, it acts like one. Change the title, let people comment on it, &c

stuff we dig

FYI: yes, the above are affiliate links, so we do make a small amount of money if you follow them over and buy what they're selling. Thank you for helping pay our hosting costs!

Posted in

Dave Kuhar

Dave is the jack-of-all-trades running Transmit Studio, a media creation lab in San Jose. He's built more websites than he can remember, well over a hundred of them on WordPress. Whether it's tradeshow booths, video/TV, graphic design, presentations, magazine, tearsheet, and catalog layout, corporate identity packages, or just about any other kind of visual communication project, he's worked on it!

1 Comment

  1. Jude Love on October 17, 2017 at 10:29 pm

    Invaluable help Dave – thanks again

Leave a Comment