Separate Media Library for each user

I’m creating a theme for a client and one of their requests is to allow users to upload images for their own use, but there may be an option to share the images.

Here are some of the caveats:

Read More
  • Each user has the ability to upload their own images to their own Media Library.
  • These images must be capable of being ‘tagged’ exactly similar to how we ‘tag’ ‘posts’ now.
  • The Admin can manage these images also.

Firstly is this possible and is it possible to create a ‘tag’ taxonomy for images?

Thanks for your help.

Related posts

Leave a Reply

3 comments

  1. Built in features

    The Media Library has major updates with the upcoming version. You can see the changes in the slides by Daryl Koopersmith here. You can read the announcement and discussion on “Make”.

    Your request for “tags/categories” is already built into 3.5.

    Note

    The difference between themes and plugins is pretty easy: Display vs. Functionality. So I’d suggest to not build such functionality into a theme, but keep it separated as plugin. Your benefit from doing this: You/Your client wouldn’t loose this functionality on update or theme change. It simply makes your life a lot easier. Imagine, that you got another client requesting this. Then you’d simply upload your already existing plugin instead of ripping this out of your theme and moving it into the next theme.

  2. As Kaiser mentioned, in the next version of WordPress the Media Library uses a UI closer to the posts/pages interface.

    Since the media library is basically a CPT called ‘attachment’, you can comment on, have post meta, and assign taxonomies to the ‘attachment’ post type.

    3.4.2 supports all of this, but will not show the taxonomies UI for media types. 3.5 does.

    So that’s tagging done.

    For showing all to admins, and giving individual users their own media libraries? You’ll need to filter them out based on ‘author’, using a pre_get_posts filter that only runs in the backend, and if the user is not of role administrator or higher, and only if the query is looking for posts of type ‘attachment’.

  3. This Q&A shows how to limit the posts a user role is able to see in the dashboard. It was missing the attachments part, which is this:

    add_filter( 'pre_get_posts', 'wpse_72278_current_author_media' );
    add_filter( 'views_upload', 'wpse_72278_custom_view_count', 10, 1 );
    
    function wpse_72278_current_author_media( $query ) 
    {
        global $pagenow, $user_ID;
    
        if( 'upload.php' !== $pagenow )
            return $query;
    
        if ( $query->is_admin && current_user_can( 'editor' ) ) 
            $query->set( 'author', $user_ID );      
    
        return $query;
    }
    
    function wpse_72278_custom_view_count( $views ) 
    {
        global $user_ID, $wpdb;
    
        if ( !current_user_can('editor') ) 
            return $views;
    
        $total = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->posts 
            WHERE post_author = '$user_ID'
            AND post_type = 'attachment' " );
        $image = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->posts 
            WHERE post_author = '$user_ID' 
            AND post_mime_type LIKE 'image/%' " );
        $video = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->posts 
            WHERE post_author = '$user_ID' 
            AND post_mime_type LIKE 'video/%' " );
        $detached = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->posts 
            WHERE post_author = '$user_ID' 
            AND post_type = 'attachment' AND post_parent = '0' " );
    
        $views['all'] = preg_replace( '/(.+)/U', '('.$total.')', $views['all'] ); 
        $views['image'] = preg_replace( '/(.+)/U', '('.$image.')', $views['image'] ); 
        $views['video'] = preg_replace( '/(.+)/U', '('.$video.')', $views['video'] ); 
        $views['detached'] = preg_replace( '/(.+)/U', '('.$detached.')', $views['detached'] ); 
    
        return $views;
    }
    

    This other takes care of the issue of a post owned by one author and the upload made by another.

    And case you’re not jumping into 3.5 yet, this last one handles Custom Fields for Attachments.