skip to Main Content

I created a custom button for uploading images to wordpress media. How to hide the hole "mediaLibrary" Section in the Media Upload Modal?. I tried the following code which only removes the tab title not the hole tab. There are a lot of answers claiming that the following removes the tab – but it only removes the tab title. Any idea how to solve this issue?

Following code only removes the tab title

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

Some visual to make it clear for everyone

Remove the following section

enter image description here

Keep the following section

enter image description here

2

Answers


  1. When you switching to media library tab, starts ajax query for load attachments. You can try add this action with 1 priority.

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

    Also you can hide tab with css or remove html with js.

    Login or Signup to reply.
  2. I’m making a support feature plugin where the user is only allowed to upload media to custom post. The attached media will then be displayed in a metabox along with a button that opens the media uploader. So, I have the same problem: I only want the "Upload files" tab and no ability to select existing media via the "Media Library" tab.

    First, I don’t have to do any extra work to attach the file to the post provided I pass the post ID during admin_enqueue_scripts enqueuing of the media library:

    wp_enqueue_media(array(
        'post'  => isset($_GET['post']) ? $_GET['post'] : NULL,
    ));
    

    Second, I attach a listener to the media library on it’s opening to hide the "Media Library" tab and to activate the "Upload files" tab and display its contents (this is javascript that acts on the object created by wp.media({})):

    my_frame.on('open', function(){
        jQuery(".media-menu-item#menu-item-browse").hide();
        jQuery(".media-menu-item#menu-item-upload").trigger('click');
    });
    

    Lastly, when the user uploads a file, the Media Uploader displays the "Media Library" content and awaits the user to select that media. The point is to hide the "Media Library" content so displaying it after upload completely defeats the purpose. When I first read this answer over a week ago, I had no idea what to do. Today, I ran across a freakishly weird javascript trigger that is called {region}:activate:{mode} (yeah, I know).

    my_frame.on('content:activate:browse', function(){
        my_frame.close();
    });
    

    The "content" region is the main viewport under the tabs. When that region changes, it go through a series of triggers (one being "activate"). The stuff displayed in the viewport is the mode: "browse" is the mode for the "Media Library" tab; and "upload" is the mode for the "Upload files" tab). And, that’s how the trigger fired when the content of the Media Library is displayed gets its name (content:activate:browse).

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search