I am developing a plugin in which i am required to allow front end users to upload media, which I am able to do. But I don’t want to let them sneak into media library which comes as tab with uploader screen.
I tried the following
function remove_medialibrary_tab($tabs) {
if ( !current_user_can( 'administrator' ) ) {
unset($tabs['library']);
return $tabs;
}
else
{
return $tabs;
}
}
add_filter('media_upload_tabs','remove_medialibrary_tab');
But seems this filter is deprecated and doesn’t works anymore.
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);