The Problem With Image Attachments In WordPress

Something I’ve wanted to see in WordPress for a long time is the ability to determine which posts an image is being used on. WordPress 4.0 is expected to ship by the end of August and contains two subtle changes to image data in the media library. You’ll be able to view the author who uploaded the image and the post parent ID it’s attached to.

Uploaded By and Uploaded To Informatiom
Uploaded By and Uploaded To Information

I find this information to be useful, especially for multi-author websites. Although you can view the parent post ID the image is attached to, you can’t locate each post the image is used on. It’s important to note the difference between an image attached to a post and using an image already available in the media library.

Why Knowing Where An Image Is Used Comes In Handy

Let’s say I use a featured image for a post and it’s the featured image for several other posts. If I want to change the featured image in one post and have it change in every other post, I can’t. I also can’t change the image for specific posts automatically, it’s either all or nothing. I can’t determine which posts are using the image because WordPress only sees it being used on the parent post. This is due to the attachment relationship.

In other words, the first post the image is attached to is the only information WordPress has on where it’s being used. I could browse to every post and manually change it but my memory isn’t that good and it’s a tedious process.

The History Behind The Dilemma

In trying to figure out why this is, Justin Tadlock pointed out two Trac tickets, 10657 and 14513. Both contain lengthy, technical discussions, on how to add support for many-to-many relationships between posts and attachments in WordPress. Within the discussions, the Posts 2 Posts plugin is commonly referred to as a top-notch plugin that offers this functionality.

Before the Community Summit in October of 2012, Andrew Nacin and a host of other WordPress developers came together to discuss the subject in detail. The ideas brought forth from that conversation are available on the Make.WordPress.com blog and is a potential roadmap for taxonomy meta and post relationships in WordPress.

It’s Possible But Not Worth The Hassle

WordPress automatically renames files if it detects the same name being used in the media library. This means you can’t upload a new image with the same file name to replace it. An alternative method is to use FTP to locate the original file and overwrite it, keeping the same name. The image will change in all posts using it, regardless of the attachment details. You’ll also need to overwrite any images that were resized automatically by WordPress or else they’ll show up instead of the new ones.

After performing all sorts of tests in my local environment for trying to change one image across multiple posts, I’ve decided it’s too much of a hassle. Not being able to manage these relationships in WordPress doesn’t make sense. Ideally, I’d like to be able to determine which posts are using an image and replace that image across all of those posts at the same time. I also wouldn’t mind the flexibility to change an image for certain posts while leaving the others alone.

19 Comments


  1. This is fabulous. I’ve been wanting this for a long time. I hope, if I understand correctly, that I will be able to see if I have an image that isn’t being used at all. If I added an image into a page or post, will it show it’s being used there? Or does it specifically have to be “attached”?
    I always want to see who uploaded it as well. I’m a clean freak and if there’s old images sitting in the library not being used, I want them gone.

    Report


    1. Some of the things you want are already available in WordPress. If you browse to the WordPress media library and select Unattached, you’ll be able to see which images are not attached to anything.The Uploaded By and Uploaded To information is coming in WordPress 4.0. What I want is the ability to see every post that is using an image. Right now, I can only see the post the image is attached to. There is a disconnect between the two which is what I’m hoping will be solved with improvements to the way post relationships are handled .

      Report


  2. I would like a search filter: all images set to ‘featured’.

    Sometimes you want to change theme, and the featured image looks stupid. I’ve tried and suggested that but I don’t think they’ve seen it.

    Report


    1. Is there a trac ticket for that? Maybe there was an update in the meantime.

      Otherwise, Sergey’s plugin (see his comment below) could be extended to use a filter like this.

      Report


      1. I’m a somewhat inexperienced user, and I think it was in WordPress.COM I did it, now that I think of it.

        Imported my wp.com blog and now all my photos are unattached. It’s a sad situation.

        I read about the plugin below, but didn’t really get it — how to install it.

        Report


  3. In regards to replacing/updating media have you tried the Enable Media Replace plugin (http://wordpress.org/plugins/enable-media-replace/)? It allows you to update and replace media just like FTP’ing and replacing the file on the server – but all within the WordPress backend.

    Report


  4. I can fully relate to the frustration about media management in WordPress.

    Why can’t an image be attached to multiple posts? Because there are functions that return the ID of the “parent” post/page, so they would have to be altered.
    However, it is not like something like this hasn’t been done before. A post can have multiple tags and categories, but an image can’t belong to multiple posts? Pffff…

    Thankfully there is this plugin, which comes in handy from time to time, though it doesn’t really solve the big problem:
    http://wordpress.org/plugins/unattach-and-re-attach-attachments/

    Report


  5. > Although you can view the parent post ID the image is attached to, you can’t locate each post the image is used on.

    Your post inspired me to create a plugin which does that.

    It adds a “Used In” column to the list view in Media Library, as well as to the attachment details modal.

    It can find posts that use the image (including any of its intermediate sizes) in post content, or as a featured image. Works with custom post types too.

    Report


    1. Holy smokes, the Awesomesauce award goes to you! I’m going to try it out today and let you know how it goes. How do I download the code as a Zip file or plugin?

      Report


      1. You should be able to download the raw data and paste it to a new textfile with a .php extension.

        Report


      2. Thanks! I’ve submitted the plugin to the WordPress.org directory. Once approved, it should be available there.

        Report


      3. I noticed :) I’m currently testing it. So if this was so easy to accomplish in a plugin, why do you think core doesn’t already do this?

        Report


      4. I think it’s plugin territory, for now at least.

        1. A lot of users use a unique image in each post, so they probably don’t need this feature.

        2. Without the API changes discussed in the tickets linked from the article, the plugin has to perform several search queries for each attachment displayed on the current Media Library page (or in the media modal). I’ve made sure the queries are as light as possible, and they work fine in my testing. However, they still might not scale well and can potentially affect performance on large sites with multiple custom image sizes.

        Report


  6. Technically there already is a many-to-many relationship between posts and media items (as far as Featured Images go), but it’s not something inherent to posts in general.

    The trouble is that while media library entries are technically posts, and do act like objects in certain contexts, they’re still largely treated as an analog for a static physical file with some metadata on top. This means that much of the UI is focused on making the file upload process easy and possible on-the-fly, and the metadata is minimized and when possible hidden.

    So, if we want to start treating media library entries more like objects with proper recognition of their many-to-many relationships with posts, I feel like there’d need to be a significant re-thinking of how they are treated in the WP Admin. Otherwise it’s either going to complicate the existing upload process (which would be bad for average users) or result in a disjointed and unintuitive process.

    Report


  7. This would be wonderful to have. I run a number of sites that share the same image/word doc/pdf across several pages or posts. It is a true pain when one of these documents needs to be updated.

    Report


  8. LOL plenty of plugins to do these things. Spoiled for choice.

    Report


  9. Hi Jeff, Thanks for the article.

    Have you seen instances where the image details of an attachment that is created in the post not reflect updates to the metadata, say, caption and alt, performed through the media library?

    Is this the correct behavior such that the attachment to the post is particular and can retain its own, distinct metadata apart from the same attachment in the media library? Or is this a bug?

    Thanks again!

    Report

Comments are closed.