Trusted WordPress tutorials, when you need them most.
Beginner’s Guide to WordPress
WPB Cup
25 Million+
Websites using our plugins
16+
Years of WordPress experience
3000+
WordPress tutorials
by experts

How to Easily Organize Media Uploads by Users in WordPress

After running a multi-author blog for years, we’ve found that there are situations where organizing media uploads by user becomes important. In our experience, as your team of contributors grows, so does the potential for media management chaos.

We’ve encountered scenarios where authors accidentally modified or deleted each other’s images, leading to broken posts. In one instance, a misplaced click resulted in the deletion of a key visual, causing a delay in publishing an important article.

To prevent this, you can restrict or organize media uploads by users in WordPress. This feature is not offered by default, so we’ll show you our go-to way to do this.

How to organize media uploads by users in WordPress

Why Restrict Author Access to Media Uploads?

If you have a multi-author WordPress blog, then your authors might upload lots of different images. This can make it difficult for an author to find the right image, or they might delete or edit another person’s media file by accident.

This can cause all sorts of problems, including poor productivity, lots of extra work for site admins and editors, and a complicated editorial workflow.

This unlimited access can also be a privacy concern. For example, if you are working on a new product or blog post idea, then other authors might see confidential images in the media library before you make a public announcement.

If you have a WordPress membership site, then contributors may even be able to access premium media files via the media library.

That being said, let’s take a look at how to restrict who can see media uploads inside your WordPress admin area.

How to Organize Media Uploads by Users With WPCode

Back then, we found a plugin that you could use to stop users without post-editing permissions from accessing other users’ files in the WordPress media library. While this plugin was easy to use, it’s no longer updated.

Plus, the plugin can be quite limited, as you can only stop users without the edit_others_posts permission (site admins and editors) from accessing the media files of other users.

If you are a site administrator, there may be a situation where you may want to limit all users (except yourself) from accessing other people’s media files.

In that case, you can add custom code to your WordPress blog instead. We’ll show you two code snippets: one that restricts media file access for non-admin users and the other for users without the edit_others_posts permission.

Often, guides will ask you to add custom code to your WordPress theme. However, this isn’t recommended, as simple mistakes or typos in your code can cause common WordPress errors or even break your site completely.

That’s why we recommend WPCode.

WPCode is the best code snippets plugin used by over 1 million WordPress websites. It makes it easy to add custom code in WordPress without having to edit the functions.php file.

The first thing you need to do is install and activate the free WPCode plugin. For more details, see our step-by-step guide on how to install a WordPress plugin.

Upon activation, head over to Code Snippets » Add Snippet.

How to add custom PHP snippets to your site using WPCode

Here, you will see all the ready-made snippets you can add to your website. These include snippets that allow you to completely disable WordPress comments, deactivate image attachment pages, and more.

Simply hover your mouse over ‘Add Your Custom Code’ and then select ‘Use snippet.’

Adding a custom code snippet to WordPress

To start, type in a title for the custom code snippet. This can be anything that helps you identify the snippet in the WordPress dashboard.

After that, open the ‘Code Type’ dropdown and select ‘PHP Snippet.’

Restricting access to the media library using WPCode

In the ‘Code Preview’ area, you can paste one of the following code snippets:

Option 1: Restricting Media File Access for Non-Admin WordPress Users

add_filter( 'ajax_query_attachments_args', 'user_show_attachments' );

function user_show_attachments( $query ) {
    $user_id = get_current_user_id();
    // Check if the current user is not an administrator
    if ( $user_id && !current_user_can('administrator') ) {
        $query['author'] = $user_id;
    }
    return $query;
}

This code checks if the current user is not an administrator. If they’re not an admin, it filters the media library to show only the files that the user has uploaded themselves.

This means regular users can only see and manage their own media files, while administrators can still see and manage all files.

Option 2: Restricting Media File Access for WordPress Users Without Post Editing Permissions

add_filter( 'ajax_query_attachments_args', 'user_show_attachments' );
 
function user_show_attachments( $query ) {
    $user_id = get_current_user_id();
    // Checks if the current user is logged in (i.e., $user_id is not 0) and does not have the capabilities to activate plugins or edit others' posts.
    if ( $user_id && !current_user_can('activate_plugins') && !current_user_can('edit_others_posts
') ) {
        $query['author'] = $user_id;
    }
    return $query;
} 

This code is a bit more specific. It checks if the current user is logged in and doesn’t have permission to activate plugins or edit other people’s posts. If a user meets these conditions, they can only see and manage their own media files.

This allows administrators and editors to see all files, while authors and contributors can only access their own uploads.

Next, just scroll to the ‘Insertion’ section. WPCode can add your code to different locations, such as after every post, on the front end only, or admin only.

To use the custom PHP code across your entire WordPress website, click ‘Auto Insert’ if it isn’t already selected. Then, open the ‘Location’ dropdown menu and choose ‘Run Everywhere.’

Running custom PHP code across your website using WPCode

After that, you are ready to scroll to the top of the screen and click on the ‘Inactive’ toggle so it changes to ‘Active.’

Finally, click on ‘Save Snippet’ to make the PHP snippet live.

How to restrict access to media files using code

Now, users will only have access to the files they upload to the WordPress media library.

Learn More Ways to Manage WordPress Files

Besides restricting media uploads in WordPress, you may also want to check out the guides below to better manage your files:

We hope this article helped you better organize media uploads by users on your WordPress site. Next, you can check out our expert picks of the best WordPress slider plugins and our guide on how to easily lazy load images in WordPress.

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.

Disclosure: Our content is reader-supported. This means if you click on some of our links, then we may earn a commission. See how WPBeginner is funded, why it matters, and how you can support us. Here's our editorial process.

Editorial Staff

Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi with over 16 years of experience in WordPress, Web Hosting, eCommerce, SEO, and Marketing. Started in 2009, WPBeginner is now the largest free WordPress resource site in the industry and is often referred to as the Wikipedia for WordPress.

The Ultimate WordPress Toolkit

Get FREE access to our toolkit - a collection of WordPress related products and resources that every professional should have!

Reader Interactions

9 CommentsLeave a Reply

  1. Joshua Owolabi

    I really enjoy all your tutorials kudos to you.
    Although I am unable to use to build something due to lack of resources but you are WELDONE

    • WPBeginner Support

      Glad our tutorials are helpful :)

      Admin

  2. Teddu

    I would like users to be able to upload content, and once its approved go onto a new page. the user could then edit it once it is there.

    Think, similar how airbnb let users upload photos and descriptions of their holiday homes, and it appears in a page.

    do you know a plug in for this functionality?

  3. WPBeginner Staff

    It is certainly possible but can potentially open up your site to spam, malicious hack attempts. Please see out article on how to allow users to submit posts in WordPress. You can allow users to upload media without registration.

  4. fredtgorski

    The one thing I would really(!) love to see on media library would be the ability to search media per post and/or date of upload. Every time I need to reuse an image I uploaded for a very old post, it is painfull to find it again. Sometimes I simply give up and reupload it…

  5. agsm

    sir , i want to design a wordpress page where user will upload some documents with following feilds (name,e-mail,title,summary and file upload) after submit , it should appear in dashboard and when admin approve it should be show on page (all listing of upload details which has been approved by admin ) , its possible,

    thanks in advance!!!

  6. Tony Franco

    Very nice! Thank you!! :)

Leave A Reply

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.