HomeArticles

Show only user's images in media library

Stefan Baumgartner

Written by @ddprrt

More on Wordpress

This short snippet restricts authors to only view the files from the media library which they have uploaded:

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('update_core') )
$wp_query_obj->set('author', $current_user->ID );
return;
}

Add this part to your functions.php or include it in a seperate plugin

More articles on Wordpress

Stay up to date!

3-4 updates per month, no tracking, spam-free, hand-crafted. Our newsletter gives you links, updates on fettblog.eu, conference talks, coding soundtracks, and much more.