Disable media library tab for non admins in uploader screen

This function will not show media library tab in upload screen

   function remove_medialibrary_tab($strings) {
        if ( !current_user_can( 'administrator' ) ) {
            unset($strings["mediaLibraryTitle"]);
        return $strings;
        }
        else
        {
            return $strings;
        }
    }
    add_filter('media_view_strings','remove_medialibrary_tab');

I found out that switching to media library tab actually call this ajax action query-attachments. So i added another callback function to this action with top priority, which checks if user is not admin , the action halts right there. This did the trick for me 🙂

function restrict_non_Admins(){

        if(!current_user_can('administrator')){
            exit;
        }
    }

add_action('wp_ajax_query-attachments','restrict_non_Admins',1);
add_action('wp_ajax_nopriv_query-attachments','restrict_non_Admins',1);

Leave a Comment