How to Only Show Images that the User Uploaded in WordPress

Here’s how to only show the current user-uploaded images in WordPress in the Media library.

Add this Code to functions.php or a Custom PHP Plugin

To hide all media except those a user has uploaded personally, paste the following code into your theme’s function.php – or any other place you use to insert custom PHP code. If you don’t know how to do this, check out my tutorial on how to add code to WordPress using a custom plugin.

add_filter( 'ajax_query_attachments_args', 'only_show_user_images' );

function only_show_user_images( $query ) {

    $current_userID = get_current_user_id();
    if ( $current_userID ) 
        $query['author'] = $current_userID;
    return $query;
}
add_filter( 'ajax_query_attachments_args', 'only_show_user_images' );

This code does the following:

  • Hooks into the query that displays attachment information via the “ajax_query_attachments_args” filter.
  • Checks to see if the current user has admin permissions
  • Alter the query variable to restrict the user ID to the current user ID
  • Returns the altered query for processing

Allow Admins to See Everything

If in the above snippet, you want to allow admins to view all the photos – even those that they haven’t uploaded on their own, you can change the following line:

if ( $current_userID ) 

To:

if ( $current_userID && !current_user_can('manage_options'))

Testing to See if it Works

After saving this code, I opened up the media page using a new “author” user I created.

The result is a blank page. This is because the newly created author hasn’t uploaded anything, and they will only see an image if they have put it there. And that solves our problem. In the same way, every user will see only their uploads.

When using WordPress with multiple authors, many things need to be considered. The default permissions system leaves much to be desired, and we have to take care of things such as users being able to view the posts submitted by others to ensure that random people don’t have permission to mess things up on your site.

Why Segregate User Images from Each Other?

When we write posts, we can still see the images that other people have uploaded. This might not seem a big deal, and it hardly matters in most cases. After all, these images are helpful resources that anyone can upload to their posts.

But sometimes, we need to segregate these as well. For example, you might be working on a secret infographic that will differentiate your page, and you don’t want your dozens of guest authors getting a hold of it before it’s ready for publication. Ideally, we’d all like to trust those who contribute to our site, but it’s never a bad thing, to be sure. The last thing you want is for your hard work to be published elsewhere without credit!

Another use case is if you’ve purchased special image rights for some work and protected them on the front end by some copy protection mechanism. That’ll pose a barrier to those visiting your site but not to others at the WordPress backend. They may not even reuse the image in bad faith, and they might assume it’s free and use it unauthorized.

There might be many other reasons to restrict users to only viewing the media they have personally uploaded. This tutorial will show you how to achieve this with only a few lines of code.

But we also should be careful. We want the administrator or the superuser to have full rights to view the images everyone else has uploaded. So, we’ll need to consider that. Luckily, we have an easy function for that.

About Bhagwad Park

I've been writing about web hosting and WordPress tutorials since 2008. I also create tutorials on Linux server administration, and have a ton of experience with web hosting products. Contact me via e-mail!

Comments

  1. Parth Prajapati says

    Thank !! It works for me

    Reply

Speak Your Mind

*

WP-Tweaks