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

How To Restrict Access To User’s Own Uploads On WordPress Media Library
How To Restrict Access To User’s Own Uploads On WordPress Media Library

By default, WordPress allows the 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 code will allow you to Restrict Access to User’s Own Uploads on WordPress Media Library.

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;
} 

If any problem occur let us know

Spread the love

You Might Also Like

Leave a Reply

Your email address will not be published.