Skip to content

Instantly share code, notes, and snippets.

@bencooling
Last active August 9, 2017 08:38
Show Gist options
  • Save bencooling/3990625 to your computer and use it in GitHub Desktop.
Save bencooling/3990625 to your computer and use it in GitHub Desktop.
Prevent Authors from seeing other authors media files
// Prevent Authors from seeing other authors media files
add_filter('parse_query', 'my_parse_query' );
function my_parse_query( $wp_query ) {
// Only applicable for Media Library && Media Upload Dialog
if (! preg_match('/wp-admin\/(?:upload|media-upload|admin-ajax).php/', $_SERVER['REQUEST_URI']) ) {
return;
}
global $current_user;
$user_roles = $current_user->roles;
$user_role = array_shift($user_roles);
if ($user_role && is_string($user_role) && strtolower($user_role)==='author') {
$wp_query->set( 'author', $current_user->id );
}
}
@bencooling
Copy link
Author

Thanks for the heads up with ACF. I'll look into that when I get a chance! READ: Not for a while LOL

@edyzen
Copy link

edyzen commented Nov 9, 2014

Hello
I already put this code into function.php
Right, another author only can view their own images.
But , as administrator I can not view all images in media library.
No one or more images display in media library as administrator.
Any idea ?
my site is http://www.propertypilihan.com

@edyzen
Copy link

edyzen commented Nov 23, 2014

I am already upgrade wordpress to latest version , and as administrator I can see all images.
Your code running.
Thanks

@sutefu23
Copy link

sutefu23 commented Aug 9, 2017

As hlotvonen said, this code seems to conflict ACF .
So I recommend you to add .
if($wp_query->query_vars['post_type']!=='attachment'){ return; }

Regards.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment