Change WordPress user roles and capabilities › Forums › How to or FAQ › Hide specific images or images by specific user role or specific user ID
Tagged: hide content, hide images, media library
- This topic has 1 reply, 2 voices, and was last updated 4 years, 7 months ago by Vladimir.
-
AuthorPosts
-
15/04/2020 at 19:28 #6766akamotuParticipant
Hi,
I have the pro version of the plugin and have been trying to hide certain items from the media library only. I have uploaded several images (i.e. logos) that should not be seen, editable or delete able by other users. However, I need to make sure that other users can still see all other items in the media library. I have tried restricting view post and edit post settings and prohibited by both post ID and by user id which disables opening the attachment page and disables the edit option in the media library but the items still show in the media library. Can I hide those specific items from selected user roles somehow (i.e. hide postID:40 for user role: event-admin & event-superadmin, or hide all attachments by authorID:1 for user role: event-admin & event-superadmin, or hide all attachments by user role:admin for user role: event-admin & event-superadmin – any of these would work in my case). Thank you!
20/04/2020 at 02:40 #6776VladimirKeymasterHi,
You can not make this via URE Pro user interface. Try this custom code. It hides all medial library items uploaded by user with ID=1 from users with roles event-admin or event-superadmin:
add_action('pre_get_posts', 'hide_attachments_for_role', 99); function hide_attachments_for_role( $query ) { global $pagenow, $wpdb; if ( $pagenow !== 'upload.php' ) { return; } if ($query->query['post_type']!=='attachment') { return; } $user = wp_get_current_user(); if ( $user->ID==1 ) { return; // do not restrict super admin } if ( ! (in_array( 'event-admin', $user->roles ) || !in_array( 'event-superadmin', $user->roles ) ) ) { // do not hide media items uploaded by admin from roles others than above return; } $sub_query = "SELECT ID FROM {$wpdb->posts} WHERE post_type='attachment' AND post_author=1"; $admin_list = $wpdb->get_col($sub_query); if ( empty( $query->query['post__in'] ) ) { if ( empty( $query->query['post__not_in'] ) ) { $query->set( 'post__not_in', $admin_list ); } else { $list1 = array_merge( $query['post__not_in'], $admin_list ); $query->set( 'post__not_in', $list1 ); } } else { $list1 = array(); foreach ( $query->query['post__in'] as $id ) { if ( !in_array($id, $admin_list)) { $list1[] = $id; // leave just allowed items } } if (empty( $list1 ) ) { $list1[] = -1; } $query->set( 'post__in', $list1 ); } } // end of hide_attachments_for_role()
-
AuthorPosts
- You must be logged in to reply to this topic.