Restrict editors when viewing images uploaded by other editors in Wordpress media gallery

StackOverflow https://stackoverflow.com/questions/14134595

  •  12-01-2022
  •  | 
  •  

Question

Hi I have this code in my functions.php and its working well when the editor is viewing the Media library: http://example.com/wp-admin/upload.php

add_action('pre_get_posts','users_own_attachments');

function users_own_attachments( $wp_query_obj ) {

global $current_user, $pagenow;

if( !is_a( $current_user, 'WP_User') )
    return;

if ( 'upload.php' != $pagenow ) 
    return;

if( !current_user_can('delete_users') )
    $wp_query_obj->set('author', $current_user->ID );

return;
}

The above code will prevent other editors from viewing the images uploaded by other editors in the media library. Only the administrator can see all images uploaded. The editor can only see the images uploaded by themselves (not others).

Now here is the issue, when creating or editing a post, Wordpress has an "Add Media" button that will allow users to upload images to their gallery. But the code above does not work when the user will upload via "Add Media" popup. They can still see the images uploaded by other editors.

How is it possible to modify the above code so that it will still work when users are viewing images via the "Add Media" popup and not the media library? Thank you so much for your help.

Was it helpful?

Solution

I have my answers here: https://wordpress.stackexchange.com/questions/78084/prevent-other-editors-from-viewing-images-of-other-editors-in-add-media-popup

The strategy is to add a conditional tag that will check if the user is viewing the add media popup.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top