I have 4 roles in my platform : administrator, author, editor, external
I need to restrict WordPress media library access to the user’s own uploads only for one users role (external) on my platform. The External user role can only see/select his own media.
All others roles still have access to all medias in the library.
I found the below snippets but it works for all the users :
<?php // Limit access to media library (users can only see/select own media) //
add_filter( 'ajax_query_attachments_args', 'wpsnippet_show_current_user_attachments' );
function wpsnippet_show_current_user_attachments( $query ) {
$user_id = get_current_user_id();
if ( $user_id && !current_user_can('activate_plugins') && !current_user_can('edit_others_posts')) {
$query['author'] = $user_id;
}
return $query;
}
?>
Thanks in advance for your reply.
I have 4 roles in my platform : administrator, author, editor, external
I need to restrict WordPress media library access to the user’s own uploads only for one users role (external) on my platform. The External user role can only see/select his own media.
All others roles still have access to all medias in the library.
I found the below snippets but it works for all the users :
<?php // Limit access to media library (users can only see/select own media) //
add_filter( 'ajax_query_attachments_args', 'wpsnippet_show_current_user_attachments' );
function wpsnippet_show_current_user_attachments( $query ) {
$user_id = get_current_user_id();
if ( $user_id && !current_user_can('activate_plugins') && !current_user_can('edit_others_posts')) {
$query['author'] = $user_id;
}
return $query;
}
?>
Thanks in advance for your reply.
add this to your functions.php or in a snippet, with this code the users with role external will only see their own uploads, any other role will see them all just has before.
add_filter( 'ajax_query_attachments_args', 'role_external' );
function role_external( $query ) {
$user_id = get_current_user_id();
if ( $user_id && current_user_can('external') ) {
$query['author'] = $user_id;
}
return $query;
}
to protect your image folder try use this
Add a simple .htaccess file in your site folder with the following lines
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^http://www\.your-domain\.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www\.your-domain\.com$ [NC]
RewriteRule .*\.(wav|swf|jpg|jpeg|gif|png|bmp|js|css)$ - [F,NC,L]
Note I added also js and css file even if I think it's bizzare to find someone who attempts to scrape them.
this answer can be found here