Site icon Hip-Hop Website Design and Development

Restrict WordPress Media Library for a specific user role (users can only see/select own media)

I have 4 roles in my platform : administrator, author, editor, external

I need to restrict WordPress media library access to the user’s own uploads only for one users role (external) on my platform. The External user role can only see/select his own media.

All others roles still have access to all medias in the library.

I found the below snippets but it works for all the users :

<?php // 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;
   }
?>

Thanks in advance for your reply.