Skip to content

Instantly share code, notes, and snippets.

@viankakrisna
Created November 17, 2015 09:36
Show Gist options
  • Save viankakrisna/7e9467d6d3ab7f4b76cc to your computer and use it in GitHub Desktop.
Save viankakrisna/7e9467d6d3ab7f4b76cc to your computer and use it in GitHub Desktop.
Restrict non admin to only show image that they upload
add_action( 'pre_get_posts', 'ml_restrict_media_library' );
function ml_restrict_media_library( $wp_query_obj ) {
global $current_user, $pagenow;
if (!in_array( 'administrator', $current_user->roles )){
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;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment