如何限制媒体库对WordPress中用户自己上传的内容的访问

By default, WordPress allows authors to see all images on your site’s media library. This could be problematic if you invite a lot of guest authors. In this article, we will show you how to restrict WordPress media library access to user’s own uploads.

默认情况下,WordPress允许作者​​查看您网站媒体库中的所有图像。 如果邀请许多来宾作者,这可能会出现问题。 在本文中,我们将向您展示如何限制WordPress媒体库对用户自己的上传内容的访问。

Restrict WordPress media library access to user's own uploads
为什么限制媒体库访问用户自己的上载? (Why Restrict Media Library Access to User’s Own Uploads?)

WordPress allows authors to see all files in the media library. They can also see images uploaded by an administrator, editor, or other authors.

WordPress允许作者查看媒体库中的所有文件。 他们还可以查看由管理员编辑或其他作者上传的图像。

To learn more, see our article on WordPress user roles and permissions.

要了解更多信息,请参阅有关WordPress用户角色和权限的文章

Let’s say you are creating a new post to announce an upcoming product or deal. Authors and guest authors on your website will be able to see the images you upload to that article in the media library.

假设您正在创建新帖子以宣布即将推出的产品或交易。 您网站上的作者和来宾作者将能够在媒体库中看到上传到该文章的图像。

Your uploads will also be visible on the ‘Add Media’ popup which users see when adding images to their own articles.

您上传的内容还将显示在“添加媒体”弹出窗口中,用户在将图像添加到自己的文章中时会看到。

For many websites, this may not be a big deal. However, if you run a multi-author website, then you may want to change this.

对于许多网站而言,这可能并不重要。 但是,如果您运行一个多作者网站 ,则可能需要更改它。

Let’s take a look at how to easily restrict media library access to user’s own uploads.

让我们看一下如何轻松地将媒体库访问限制为用户自己的上传。

方法1:使用插件限制媒体库访问 (Method 1: Restrict Media Library Access Using a Plugin)

This method is easier and is recommended for all users.

此方法更简单,建议所有用户使用。

First thing you need to do is install and activate the Restrict Media Library Access plugin. For more details, see our step by step guide on how to install a WordPress plugin.

您需要做的第一件事是安装并激活“ 限制媒体库访问”插件。 有关更多详细信息,请参阅有关如何安装WordPress插件的分步指南。

This plugin works out of the box, and there are no settings for you to configure.

该插件可以直接使用,没有可供您配置的设置。

Upon activation, it filters the media library query to see if the current user is an administrator or editor. If the user role does not match either of them, then it will only show user’s own uploads.

激活后,它将过滤媒体库查询,以查看当前用户是管理员还是编辑者。 如果用户角色与两个角色都不匹配,那么它将仅显示用户自己的上传内容。

Users with the administrator or editor user role will be able to see all media uploads as usual.

具有管理员或编辑者用户角色的用户将能够照常查看所有媒体上载。

方法2:手动限制媒体库访问 (Method 2: Restrict Media Library Access Manually)

The first method would work for most websites as it limits media library access and allows only administrator and editor to view all media uploads.

第一种方法适用于大多数网站,因为它限制了媒体库的访问,并且仅允许管理员和编辑者查看所有媒体上载。

However, if you want to add a custom user role or simply don’t want to use a plugin, then you can try this method instead. It uses the same code used by the plugin, but you will be able to modify it to meet your needs.

但是,如果您要添加自定义用户角色,或者只是不想使用插件,则可以尝试使用此方法。 它使用与插件相同的代码,但是您可以对其进行修改以满足您的需求。

This method requires you to add code to your WordPress files. If you haven’t done this before, then take a look at our guide on how to copy and paste code in WordPress.

此方法要求您将代码添加到WordPress文件中。 如果您以前没有做过,请查看我们的指南, 了解如何在WordPress中复制和粘贴代码

You’ll need to add the following code to your WordPress functions.php file or a site-specific plugin.

您需要将以下代码添加到WordPress functions.php文件或特定站点的插件中


// 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 current_user_can function to check if the user has the capability to activate plugins or edit other user’s posts. If they don’t, then it changes the query used to display media files and limit it to user’s ID.

此代码使用current_user_can函数检查用户是否具有激活插件或编辑其他用户的帖子的能力。 如果没有,它将更改用于显示媒体文件的查询并将其限制为用户的ID。

We hope this article helped you learn how to restrict WordPress media library access to user’s own uploads. You may also want to limit authors to their own posts in WordPress admin area.

我们希望本文能帮助您学习如何限制WordPress媒体库对用户自己的上传内容的访问。 您可能还希望将作者限制为他们在WordPress管理区域中的帖子

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.

如果您喜欢这篇文章,请订阅我们的YouTube频道 WordPress视频教程。 您也可以在TwitterFacebook上找到我们。

翻译自: https://www.wpbeginner.com/plugins/how-to-restrict-media-library-access-to-users-own-uploads-in-wordpress/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值