[wp-hackers] Splitting up the media library?

Nikola Nikolov nikolov.tmw at gmail.com
Mon Mar 21 23:04:26 UTC 2016


I was waiting for someone to give you the magic bullet that would solve
this problem, but no luck so far :( First of all that sounds like a pretty
cool(by cool I mean challenging, which for me == cool :) ) task. Here are
some abstract thoughts:

You should be able to initialize an uploader that will pass a special
parameter in the upload request(haven't looked into the JS side of this,
but I assume it would be possible one way or another). Based on that
parameter, you can hook in a few places:
 1. The 'wp_handle_upload' filter. This fires from the _wp_handle_upload()
<https://github.com/WordPress/WordPress/blob/master/wp-admin/includes/file.php#L239>
function right after an upload has been moved to it's destination and
passes an associative array containing the keys 'file', 'url' and 'type'.
If you want to get fancy you can move the file to a custom directory at
this point(maybe hook on 0 priority so that everything afterwards works
with the final URL/file path for the file). You can probably do it later
on, but this seems like the best place to do it IMHO. A quick look-up
through core seemed like pretty much all core upload handling functions
lead to _wp_handle_upload().
2. Hook to the 'add_attachment' action. You're passed the new attachment's
ID as the only parameter. I would add a custom post meta here indicating
the Custom Attachment Type(in case you want to add more in the future use
something descriptive). This is the key ingredient - based on this meta,
you could do a multitude of things:
  - Hide all attachments that have your post meta from the default Media
screen and the Add Media modal.
  - List only the attachments you need on your custom media screen.
  - Create a custom view(those things on the left hand side - "Insert
Media", "Create gallery", etc.) on the "Add Media" modal that will only
list your custom attachments.
  - Show your custom taxonomies and meta only on the custom attachments
edit screens.
3. Hook to the 'intermediate_image_sizes' filter in order to limit the
number of image sizes generated for your custom attachments to those you're
going to need/want.

And probably a bunch of other things I can't think of right now.

Ugh, I wish I had some time to work on this - it tickled my interest quite
a bit :)

Not sure if that will help you at all. Keep us posted on whether you manage
to solve this or not and good luck :)

On Fri, 18 Mar 2016 at 22:47 Mark Slade <markandrewslade at gmail.com> wrote:

> I'm working on a project where I will be attaching some custom taxonomies
> and metadata to some of the items in the media library.  This is straight
> forward.
>
> It would be preferable if I could create an entirely separate media library
> for this.  I'm thinking of something that is to the media library what
> custom post types are to posts.  That is - something that behaves exactly
> like the media library but has an admin UI distinct from the regular media
> library.  My goal is twofold:
>
> 1. A separate place to view / manage the new post type that is distinct
> from the media library.  I don't want users to have to navigate to the
> regular media library and filter through regular media library stuff versus
> assets.
>
> 2. The extra taxonomies / meta data should only apply to this new post
> type.  For example if "Color" is one of the taxonomies my assets will
> support, I want the assets UI to have a "Color" column but I *don't* want
> the regular media library to have a "Color" column.
>
> I can tell that WordPress doesn't support this without some extra work but
> I'm wondering if anyone with more experience dealing with the media library
> can weigh in.  Has anyone done this before?  Is it a bad idea?  If it's
> do-able, can anyone recommend a high-level approach?
>
> Thanks,
> Mark
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>


More information about the wp-hackers mailing list