8

I am working on a clients site, one of the features they want is for his Editors or Authors to only be able to select existing images to insert into Posts from the Media Library.

So looking at the media manager/upload screen below, is it possible to make the Upload Files tab to only be visible to Admin Users and also to make the Media Library shown by Default when this Dialog is shown?

enter image description here

3

1 Answer 1

17
+25

Here are 2 methods to disable all uploading for users that are not administrators:

Method 1:

Remove the upload_files capability from the roles you do not want to be able to upload.

e.g. removing it from author role:

$role = get_role( 'author' );
$role->remove_cap( 'upload_files' );

This is saved to the database so it should only happen once, not on every page load. Removing it from the editor role should be the same albeit with the obvious modification.

Removing the capability will have the desired effect, but it may have additional effects you may not find so desirable, the only way to be sure is to test

Method 2

Consider this code. It uses the wp_handle_upload_prefilter filter to check the user has admin status, and aborts the upload if they do not. It relies on only administrators and above having manage_options.

<?php
/**
 * Plugin Name: Admin Only Uploads
 * Description: Prevents Uploads from non-admins
 * Author: TJNowell
 * Author URI: https://tomjn.com
 * Version: 1.0
 */

function tomjn_only_upload_for_admin( $file ) {
    if ( ! current_user_can( 'manage_options' ) ) {
        $file['error'] = 'You can\'t upload images without admin privileges!';
    }
    return $file;
}
add_filter( 'wp_handle_upload_prefilter', 'tomjn_only_upload_for_admin' );

You can make use of this filter to be more specific than upload_file, e.g. preventing the upload of images, and only images, but allowing audio, etc

12
  • Doesn't this remove the entire media library? Jul 6, 2013 at 18:30
  • 2
    Hence method #2
    – Tom J Nowell
    Jul 6, 2013 at 20:03
  • Method #2 works like a charm! My setup: some images are uploaded by the admin, while other users can only view the admin's images and cannot upload their own. Cheers!
    – liviucmg
    Jan 17, 2015 at 19:12
  • my client keep uploading the wrong file types (png, bmp !). Can is topped them using these mime types but still allow myself to upload .PNGs?
    – v3nt
    Jun 16, 2017 at 9:41
  • @v3nt that's a new question, you should ask it on the site not in a comment
    – Tom J Nowell
    Jun 19, 2017 at 17:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.