By default, WordPress allows every author to view all the images in media library, which isn’t a great idea, right? Adding this snippet to WordPress’ current theme’s functions.php file will restrict users to view only media library items they uploaded in the library.
//Manage Your Media Only function mymo_parse_query_useronly( $wp_query ) { if ( strpos( $_SERVER[ 'REQUEST_URI' ], '/wp-admin/upload.php' ) !== false ) { if ( !current_user_can( 'level_5' ) ) { global $current_user; $wp_query->set( 'author', $current_user->id ); } } } add_filter('parse_query', 'mymo_parse_query_useronly' );
Doesn´t work for me. It still shows all the files when a user clicks “Add Media” button on “Add New Post” page.
It hides all the media files from the /wp-admin/upload.php directory, not the media uploader.
I think, this will do the job…
add_action('pre_get_posts','ml_restrict_media_library');
function ml_restrict_media_library( $wp_query_obj ) {
global $current_user, $pagenow;
if( !is_a( $current_user, 'WP_User') )
return;
if( 'admin-ajax.php' != $pagenow || $_REQUEST['action'] != 'query-attachments' )
return;
if( !current_user_can('manage_media_library') )
$wp_query_obj->set('author', $current_user->ID );
return;
}
Nope. I tried this snippet on a test site and it’s still showing all the media items. Try this plugin: http://wordpress.org/plugins/view-own-posts-media-only/
Thank you! The Plugin seems to do the trick for me! :)
Harshal, your code works fine in wp 4.0. Thanks!!
You’re Welcome…! :)