¿Estás buscando una forma de restringir a los usuarios la visualización de los elementos de la biblioteca multimedia que suben? Si bien es probable que haya un plugin para esto, hemos creado un fragmento de código rápido que puede utilizar para restringir a los usuarios ver sólo los elementos de la biblioteca multimedia que suben en WordPress.
Instrucciones:
Todo lo que tienes que hacer es añadir este código al archivo functions.php de tu tema o en un plugin específico del sitio:
function my_files_only( $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', 'my_files_only' );
Nota: Si es la primera vez que añades fragmentos de código en WordPress, consulta nuestra guía sobre cómo añadir correctamente fragmentos de código en WordPress, para no romper accidentalmente tu sitio.
Si te ha gustado este fragmento de código, por favor, considere la posibilidad de revisar nuestros otros artículos en el sitio como: 7 mejores servicios VPN para usuarios de WordPress y cómo crear un formulario de donación en WordPress.
we just realised that although this scripts works in web wordpress , however the android /mobile version of media library still viewable by everyone
a small change I did in code to make it work on front end on my profile page
//if ( false !== strpos( $_SERVER[ ‘REQUEST_URI’ ], ‘/wp-admin/upload.php’ ) ) {
if ($wp_query->query_vars[‘post_type’]==”attachment”){
this way it is working properly.
Thanks Prasanna, i registered a Disqus account to just thank you.
Me too
[…] some googlin’ I found many references (here, and here, for example ) to this […]
It worked! (WP 3.8.1) Thank you!
hi, thanks for the code. very helpful!
however, other users are still able to view the attachments if they have a direct link. is there a way to prevent this? as in only the owner of the media (and admin) are able to see the attachments. other users or public cant seem them.
Thanks.
I put that code… but have wrong counter for media post…
Curent user not have media post … whi have that All (15) | Images (15) | Unattached (2)
i want show count for curent user.. not for all….
I just hide all count numbers by css: subsubsub .count {display: none; }
Çok te?ekkür ederim – Thank you
Thank you a ton for this…. Role scoper was messing with another plug in and thats the only feature i wanted was the images to be limited… Thanks for updating it to work with the new media library, it works flawlessly.
Does anyone know how to restrict user also when they upload file from the insert or edit post? In fact when I press the Add Media button, the popup shows me the Media Library tab, with all the media uploaded by all users. I’m using the WP 3.5.1.
Thanks!
Look at my reply to designlabCPH above. Insert this function in your functions file, and the media library will be restricted to users’ own files only.
It doesn’t work for me 🙁 I want users that are non-admins to be able to view only their own files, also when they are creating posts, only see their own images when they add featured images, etc
It doesn’t work for me 🙁 I want users that are non-admins to be able to view only their own files, also when they are creating posts, only see their own images when they add featured images, etc
It doesn’t work for me 🙁 I want users that are non-admins to be able to view only their own files, also when they are creating posts, only see their own images when they add featured images, etc
It doesn’t work for me 🙁 I want users that are non-admins to be able to view only their own files, also when they are creating posts, only see their own images when they add featured images, etc
It works now! Thank you so much!
I used the Role Scoper plugin. I’ve been looking to do figure this out all day. Role Scoper will only allowed the logged in user to see their own content and own posts by default. You can change this if you want to as well. Private Media Library for Authors 😉
is there a way to set up this line of code to restrict the media library for contributors only. That way contributors can only see media they upload and admins can see everything.
is there a way to program this line of code to only apply this to specific membership levels? (i.e. contributors are only able to see their media uploads, but admins can see everything)
Using WP 3.3.1 and this is not working. Is their an update on the code? Thanks.
Look at my reply above. I posted a function that will work with the new media manager (WP 3.5+)
I have one question kevin, how to restrict contributor from uploading media?
Hi Rian,
On line 3 you will see “level_5” on this wordpress page is has details on what the user levels are you can just change that level. http://codex.wordpress.org/User_Levels#User_Level_5_2
i didn’t notice that default role for contributor is can’t upload any media. i just try it last night.. haha.
Thanks you very much Kevin! 🙂
No problem anytime,
Thanks Kevin, it is very useful, but how to fix ‘total number of files’ ?
Glad to hear you like the snippet! Well the total is still correct since the files do exist, although I do see your point. Ill take a look into this in the near future see if I can post a snippet.
thanks for the post. just what i was looking for.
i made a small change >> also add the file media-upload.php to this line of code. this will also filter the images /mediafiles above the wysiwyg editor.
presta
Hi Sheila,
Where/What did you post code-wise in the media-upload.php file? I’m getting an error when I paste the code in. Thanks in advance!
Found an issue, media library still showing full content at post insert media popup window. (same as other pages template too)
however, I added get global $post, and add another condition for posts.
‘/wp-admin/media-upload.php?post_id=’.$post->ID.’&type=image&tab=library’
but it didn’t work..
I fixed this by adding a second clause
“|| strpos( $_SERVER[ ‘REQUEST_URI’ ], ‘/wp-admin/media-upload.php’ ) !== false”
How on earth do you get this to work in WP 3.5?
I’ve tried everything (I belive, although I’m not a PHP coder) including adding your solution above – and I just can’t get it to work.
I need that only Admins can see all media in the library pop-up, and everyone else (including editors) can only see their respective images…. Would you mind spelling it out for me? 🙂
Use Role Scoper if you haven’t gotten it figured out yet man. It accomplishes this very issue. It’s by default when you install the plugin. Both the Media Library and posts. FYI
I have spent hours today just to fix that users upload issue, and found that’s the only code that work. Thanks!
You have to change a few parameters for this to work with the new media manager.
You can use this function (notice that I created a “manage_media_library” capability. Only if the user has this, will they see other files than their own):
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;
}
I was looking exactly for this: ‘admin-ajax.php’ != $pagenow || $_REQUEST[‘action’] != ‘query-attachments’
thanks so much!
Where did you put this?
as a new plugin! good, this works for me!
great code solution Patrick – fixed my issue pronto – I wish I had found this back in Dec. : ) thank you!
I try your code in plugin, directly in function.php but it doesn’t work. I’ve WordPress 3.5.1 ans i want to use it in front end with the add media button of the wp_editor function.
I’ve tried in wp-admin but it doesn’t work to.
I’ve you any idea why it’s not working ???
Used this, much kudos to you!
This snippets is so helpful for my multi bloggers site. You should make it to WP plug-ins! thanks =)
Cool glad you like the snippet, I’m not sure it is worth adding as a plugin although it could be setup as one.
You should, I’m having hard time in the plug-ins pool finding this and end-up gave up and looking for external resources, that causes me to reached here. I love your blog, straight cut per purpose snippet. Learning a lot from here!
You’d shined my day, Thanks Chard! =)
No problem, Glad you like the website – visit regularly we post daily!