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 Restrict Media Library Access to User’s Own Uploads in WordPress

By default, WordPress allows authors to see all images on your site’s media library. This could be problematic if you invite a lot of guest authors. In this article, we will show you how to restrict WordPress media library access to user’s own uploads.

Restrict WordPress media library access to user's own uploads

Why Restrict Media Library Access to User’s Own Uploads?

WordPress allows authors to see all files in the media library. They can also see images uploaded by an administrator, editor, or other authors.

To learn more, see our article on WordPress user roles and permissions.

Let’s say you are creating a new post to announce an upcoming product or deal. Authors and guest authors on your website will be able to see the images you upload to that article in the media library.

Your uploads will also be visible on the ‘Add Media’ popup which users see when adding images to their own articles.

For many websites, this may not be a big deal. However, if you run a multi-author website, then you may want to change this.

Let’s take a look at how to easily restrict media library access to user’s own uploads.

Method 1: Restrict Media Library Access Using a Plugin

This method is easier and is recommended for all users.

First thing you need to do is install and activate the Restrict Media Library Access plugin. For more details, see our step by step guide on how to install a WordPress plugin.

This plugin works out of the box, and there are no settings for you to configure.

Upon activation, it filters the media library query to see if the current user is an administrator or editor. If the user role does not match either of them, then it will only show user’s own uploads.

Users with the administrator or editor user role will be able to see all media uploads as usual.

Method 2: Restrict Media Library Access Manually

The first method would work for most websites as it limits media library access and allows only administrator and editor to view all media uploads.

However, if you want to add a custom user role or simply don’t want to use a plugin, then you can try this method instead. It uses the same code used by the plugin, but you will be able to modify it to meet your needs.

This method requires you to add code to your WordPress files. If you haven’t done this before, then take a look at our guide on how to copy and paste code in WordPress.

You’ll need to add the following code to your WordPress functions.php file or a site-specific plugin.

// Limit media library access
 
add_filter( 'ajax_query_attachments_args', 'wpb_show_current_user_attachments' );

function wpb_show_current_user_attachments( $query ) {
    $user_id = get_current_user_id();
    if ( $user_id && !current_user_can('activate_plugins') && !current_user_can('edit_others_posts
') ) {
        $query['author'] = $user_id;
    }
    return $query;
} 

This code uses current_user_can function to check if the user has the capability to activate plugins or edit other user’s posts. If they don’t, then it changes the query used to display media files and limit it to user’s ID.

We hope this article helped you learn how to restrict WordPress media library access to user’s own uploads. You may also want to limit authors to their own posts in WordPress admin area.

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

13 CommentsLeave a Reply

  1. Noel Williams

    Anyone looking for an update might want to consider the following which takes care of the list and grid issues

    // For list view
    add_action( ‘pre_get_posts’, ‘wpb_show_current_user_attachments_list_view’ );
    function wpb_show_current_user_attachments_list_view( $query ) {
    if ( is_admin() && $query->is_main_query() && $query->get(‘post_type’) === ‘attachment’ ) {
    $user_id = get_current_user_id();
    if ( $user_id && !current_user_can(‘administrator’) ) {
    $query->set(‘author’, $user_id);
    }
    }
    }

    // For grid view
    add_filter( ‘ajax_query_attachments_args’, ‘wpb_show_current_user_attachments_grid_view’ );
    function wpb_show_current_user_attachments_grid_view( $query ) {
    $user_id = get_current_user_id();
    if ( $user_id && !current_user_can(‘administrator’) ) {
    $query[‘author’] = $user_id;
    }
    return $query;
    }

    • WPBeginner Support

      Thank you for sharing this :)

      Admin

  2. Bruno

    The plugin works, but If you switch to ‘upload.php?mode=list’ it’s possible to see all medias again. It works only on the mode=grid

    • WPBeginner Support

      Thank you for sharing this information :)

      Admin

  3. nathan

    Amazing ! Its working !

    But if using wordpress app installed from mobile, all users still can access whole media library. Any solutions?

    Your help is much appreciated !

    • WPBeginner Support

      We do not have a solution for the app at the moment but we will certainly keep an eye out.

      Admin

  4. Alvaro Gomez

    Thank you for mentioning my plugin :)

    • WPBeginner Support

      Thank you for creating the plugin and placing it on the WordPress.org repo :)

      Admin

  5. Daniel

    Am looking for away i can make user upload directly from their pc instead of going to media page

  6. kim

    This may not be the post I need to be asking this on but….

    What if you create a role for say, teacher. Is there a way or plugin that would filter so that one teacher can’t see another teacher media files? If that makes sense? :)

    Teacher-username1- media (only see username1 media files)
    Teacher-username2-media (only see username2 media files)

  7. Hugh

    Instead of:

    !current_user_can(‘activate_plugins’)
    &&
    !current_user_can(‘edit_others_posts’)

    USE:

    !current_user_can(‘administrator’)

    Because if for example you use the ‘User Role Editor’ plugin, you might want to allow the current user to be able to activate plugins AND/OR edit others posts.

    Just a thought, works for me :)

    Thanks for the post!

  8. Peter

    Nice post.

    And is there a way to disallow uploading files directly to the media library and force users (except admins) to use Add Media button within post/page editor?

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.