Restricting User Access to the Media Library in WordPress

📢 This article was translated by gemini-3-flash-preview

By default, WordPress allows authors to see all images in your site’s media library. They can view files uploaded by administrators, editors, or other authors.

For many sites, this might not be an issue. However, if you are running a multi-author site, you might want to change this behavior.

First, navigate to site-root/wp-content/themes/your-active-theme/.

Find the functions.php file and edit it. Insert the following code at the end:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
// 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;
}

References

How to restrict media library access to user-uploaded content in WordPress

This post is licensed under CC BY-NC-SA 4.0 by the author.