At the moment, the plugin only checks for roles (admin, editor, author & contributor), not for specific capabilities. This means that any custom roles will be considered as authors in terms of permissions and will only be able to see their own uploads.
/**
* Plugin Name: Restrict Media Library Access
* Description: Restricts access for Authors, Contributors, and any role that cannot edit other users' posts so they can only see their own Media Library uploads.
* Plugin URI: https://wordpress.org/plugins/restrict-media-library-access
* Version: 1.4.1
* Author: mrfoxtalbot
* Author URI: http://mrfoxtalbot.com
* Text Domain: restrict-media-library-access
* License: GPLv2
*/
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
// Filter Ajax view
add_filter( 'ajax_query_attachments_args', 'mrfx_show_current_user_attachments' );
function mrfx_show_current_user_attachments( $query ) {
$user_id = get_current_user_id();
if ( $user_id && !current_user_can('edit_others_posts')) {
$query['author'] = $user_id;
}
return $query;
}
// Filter list view
add_filter( 'request', 'mrfx_show_current_user_attachments_list' );
function mrfx_show_current_user_attachments_list( $query ) {
$screen = null;
if ( function_exists( 'get_current_screen' ) ) {
$screen = get_current_screen();
}
if ( !is_null($screen) && in_array($screen->id, array('upload') ) ) {
$user_id = get_current_user_id();
if ( $user_id && !current_user_can('edit_others_posts')) {
$query['author'] = $user_id;
}
}
return $query;
}