WordPress Code Snippets & Development Tips – WP-Snippet.com WordPress Code Snippets & Development Tips – WP-Snippet.com

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

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