0

I have an application that requires that only the user + admin can access files uploaded by that user?

EG a user uploads an image to media. Only that user should have access to it Thanks for any tips Mike

One attempt

Second cut - screws up routing

function restrict_media_access_to_creator($query) {
    if (current_user_can('administrator')) {
        return $query;
    }
    
    $current_user_id = get_current_user_id();
    $query['author'] = $current_user_id;

    return $query;
}
add_filter('ajax_query_attachments_args', 'restrict_media_access_to_creator');

function restrict_posts_to_creator($where) {
    if (current_user_can('administrator')) {
        return $where;
    }
    
    global $wpdb;
    $current_user_id = get_current_user_id();
    $where .= " AND {$wpdb->posts}.post_author = " . $current_user_id;
    
    return $where;
}
add_filter('posts_where', 'restrict_posts_to_creator');

Other candidate plugins

WP Customer Area: This is a modular all-in-one solution to manage private content with WordPress. Sharing files/pages with one or multiple users is one of the main features provided by this plugin. Each user gets their own private page where they can add content and upload files.

FileBird – WordPress Media Library Folders: FileBird provides you with the ability to easily organize your files in the media library of your site. With this tool in hand, you can manage and arrange thousands of images, audios, videos, and other files in your media library.

WP Media Folder: This plugin offers a way to create folders in your WordPress media library. With the addon "WP Media Folder Addon" you can set user media access, which restricts media display and media management to user-owned or user-role media only. Note that this is a premium plugin.

WordPress Real Media Library: This is another premium option, but it's quite powerful. The WordPress Real Media Library provides a way to handle media files, folders, and galleries. It allows you to organize thousands of uploaded files into folders, collections, and galleries.

Other ideas

External file manager

  • Involves discovery, Can we hook it in to WPDatatables

Some way of tagging files that matches a key that is part of the user profile

We are already using Ultimate Member plugin - paid version

6
  • Access them on the front end or see them in the Media Library? Both of these are difficult & preventing people from accessing things on the front end from the WP /uploads/ directory isn't really a thing. If they have the URL they're able to access the content in that directory. So if someone copy/pastes a URL of a file they uploaded to a colleague, that colleague can see the file in the browser. If what you want is to restrict access to things IN the media library, IN the WP Dashboard that could be done, but there's quite a bit of code you'd probably need to write. What have you tried? Aug 4 at 16:31
  • Tony - thanks very much for a helpful comment. The application is a POC practice management system where it is important that one practitioner cannot view media that has been uploaded by someone else. Our PM has a feeling that WP Customer Area might offer a solution so we are investigating that. The project is under time/budget constraints so we are open to any workable solution It uses WPDataTables and the WPDT solution accesses WP media so a quick win could be any user partitioned file upload system
    – Cloudworks
    Aug 5 at 0:38
  • Just posted code of previous attempt
    – Cloudworks
    Aug 5 at 0:45
  • I'm not familiar with any of those plugins but what I have discovered in the past is with similar plugins is that IF the user has the URL, they can access what's in the media library. I only had a single directory that needed protected content and every person with access could see the items, so I then used HTTP Authentication to secure the directory and wrote a JS that would auto trigger completion of the HTTP Auth credentials IF the user had the correct access level so they'd never have to type the username/password. Aug 9 at 16:29
  • 1
    Thanks Tony! We're looking at a mod of Ultimate Member
    – Cloudworks
    Aug 12 at 12:02

1 Answer 1

0

Check this out, it might be helpful I am also looking for a good solution

https://wordpress.stackexchange.com/a/400352/220907

How to hide media uploads by other users in the Media menu?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.