TechOverall
Cz Complete is Incomplete

How to Restrict media files from Authors and Members in WordPress

Restrict media files for other users WP

0

By default, WordPress allows all authors and editors to view all downloaded files in the WordPress media library. In some cases, this can be a problem if there are a lot of guest authors on your site and you do not want them to see absolutely all the files on the site.

In this lesson, we’ll show you how to hide your downloaded media files from other authors and contributors in WordPress.

Why Restrict files from other WordPress users?

WordPress allows all users of admin with author rights to view all downloaded files in the library. Authors can also see all images uploaded by administrators and other authors and editors.

For example, you are an administrator and are preparing a publication about the launch of a new product on the site.

Any guest author of your site can go to the Media Files section and see everything that you uploaded for this publication, thereby learning what confidential information is likely to be ahead of time.

Also, all these downloads will be before the eyes, if the author simply downloads his content – all previous downloads will be visible in the file loader window.

For a regular blog, this will not be a problem. But if you have several authors on the project, there may be a need to hide information from outsiders (even authors).

Now we’ll show how easy it is to do this on WordPress.

Method 1. Restrict Media Library Access plugin

The easiest and safest way is to install a free plugin Restrict Media Library Access from the WordPress directory:

restrict media libraryThe plugin works immediately out of the box, it does not have any settings. Just install and activate it.

After activation, every time you access the Media files, the plug-in will check the access level of each user. If the user role is lower than the Administrator or the Editor, the user will only see their own downloads in the library.

If the user is an Administrator or an Editor – he/she will see all the downloads as usual.

Method 2. Close access to Media files manually

In the first method we allowed looking Administrators and Editors at all files, and for everyone else restricted access.

But if you want to restrict access to the site files to only one particular user role – you can use the second manual method.

To do this, add this code to the functions.php file of your active WordPress theme:

if you are not sure check custom code in WordPress without breaking something

 // Limit media library access
  
add_filter ('ajax_query_attachments_args', 'wpb_show_current_user_attachments');
 
function wpb_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;
}

This code uses the function current_user_can to check if the user with this role has the ability to activate plug-ins and edit other people’s posts. If the user role does not have such features, then the code restricts the display of files in the library only by downloads of that particular user.

Leave A Reply

Your email address will not be published.