在多用户WordPress站点, 如果给用户开启了编辑权限, 那么无论是一般作者还是编辑在撰写文章时都会用到添加媒体功能. 但是我们却又不想给一些级别作者添加媒体的权利或者只想每个用户只能上传并管理自己的媒体文件, 不能编辑他人的媒体.
这篇文章的教程就是教会大家如何隐藏”添加媒体”按钮与限定作者只能看到及编辑自己的媒体文件.
只要不是管理员就隐藏”添加媒体”按钮
下面这段代码设置只要权限等级不是最高等级level_10就隐藏掉”添加媒体”按钮
1
2
3
4
5
6
|
function
RemoveAddMediaButtonsForNonAdmins
(
)
{
if
(
!
current_user_can
(
'level_10'
)
)
{
remove_action
(
'media_buttons'
,
'media_buttons'
)
;
}
}
add_action
(
'admin_head'
,
'RemoveAddMediaButtonsForNonAdmins'
)
;
|
下面则是效果图
只能看见并编辑自己的媒体文件
运用以下代码, 管理员能看见并编辑所有媒体文件. 如果你也想要编辑也能看见所有媒体文件, 那么你需要改变’manage_options’, 如’edit pages’这样编辑拥有的权限.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
/**
* 只能看见自己的媒体文件
*/
function
my_authored_content
(
$query
)
{
//get current user info to see if they are allowed to access ANY posts and pages
$current_user
=
wp_get_current_user
(
)
;
// set current user to $is_user
$is_user
=
$current_user
->
user_login
;
//if is admin or 'is_user' does not equal #username
if
(
!
current_user_can
(
'manage_options'
)
)
{
//if in the admin panel
if
(
$query
->
is_admin
)
{
global
$user_ID
;
$query
->
set
(
'author'
,
$user_ID
)
;
}
return
$query
;
}
return
$query
;
}
add_filter
(
'pre_get_posts'
,
'my_authored_content'
)
;
|