fbpx
How to Restrict WordPress Media Library Access to User’s Own Uploads

How to Restrict WordPress Media Library Access to User’s Own Uploads

By default, WordPress allows authors/users to see all images uploaded to a WordPress site’s media library. This could be a problem if your site has users with individual images/galleries. This WordPress snippet will allow you to restrict WordPress media library access to the user’s own uploads.

Instructions
Add this code to your WordPress theme function.php file.

// Limit access to media library (users can only see/select own media)
add_filter( 'ajax_query_attachments_args', 'wpsnippet_show_current_user_attachments' );
function wpsnippet_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;
} 

WordPress Hosting

Leave a Reply

Your email address will not be published. Required fields are marked *

WordPress Snippet Info

Tested with Wordpress v. 5.0

How to Restrict WordPress Media Library Access to User’s Own Uploads
4.1 (81.67%) 12 vote[s]
Please report broken snippets.
Report Broken Snippet
WordPress Hosting

Recent Comments