【发布时间】:2017-03-01 09:15:58
【问题描述】:
我的站点中有 200 个用户,当从 wp-admin-> 媒体上传上传时,我需要为所有其他用户隐藏来自 user1 的所有图片上传。
如果user50上传新图片,只有他可以在媒体上传中看到。
怎么办?
【问题讨论】:
标签: wordpress
我的站点中有 200 个用户,当从 wp-admin-> 媒体上传上传时,我需要为所有其他用户隐藏来自 user1 的所有图片上传。
如果user50上传新图片,只有他可以在媒体上传中看到。
怎么办?
【问题讨论】:
标签: wordpress
1) 你可以使用这个插件 仅查看自己的帖子媒体 (https://wordpress.org/plugins/view-own-posts-media-only/)
2) 代码限制 将此 sn-p 添加到您的 wordpress 主题的 functions.php 将限制用户仅查看他们上传的媒体库项目。他们仍然会看到上传的文件总数,但即使输入了 attachment_id 也无法查看。
function my_files_only( $wp_query ) {
if ( strpos( $_SERVER[ 'REQUEST_URI' ], '/wp-admin/upload.php' ) !== false ) {
if ( !current_user_can( 'level_5' ) ) {
global $current_user;
$wp_query->set( 'author', $current_user->id );
}
}
}
add_filter('parse_query', 'my_files_only' );
3) 移除媒体标签
//Remove Media Library Tab
function remove_medialibrary_tab($tabs) {
if ( !current_user_can( 'administrator' ) ) {
unset($tabs['library']);
return $tabs;
}
else
{
return $tabs;
}
}
add_filter('media_upload_tabs','remove_medialibrary_tab');
如果当前用户不是管理员,则从将媒体添加到帖子时出现的上传/插入媒体弹出页面中删除媒体库选项卡。否则,如果用户确实具有管理员角色,那么他们仍然会看到所有选项卡(文件、URL、媒体库)
【讨论】:
我一直在寻找这个,我也遇到了一个已经过时的,这对我有用,只需将它添加到你的函数中:
add_action('pre_get_posts','ml_restrict_media_library');
function ml_restrict_media_library( $wp_query_obj ) {
global $current_user, $pagenow;
if( !is_a( $current_user, 'WP_User') )
return;
if( 'admin-ajax.php' != $pagenow || $_REQUEST['action'] != 'query-attachments' )
return;
if( !current_user_can('manage_media_library') )
$wp_query_obj->set('author', $current_user->ID );
return;
}
【讨论】:
使用pre_get_posts 操作,除管理员和编辑之外的所有人的通用限制代码是:
add_action('pre_get_posts','user_view_own_attachments');
function user_view_own_attachments( $wp_query_obj ) {
global $current_user, $pagenow;
//End the party if it isn't a valid user
if( ! is_a( $current_user, 'WP_User') )
return;
//End the journey if we are not viewing a media page - upload.php (Directly) or admin-ajax.php(Through an AJAX call)
if( ! in_array( $pagenow, array( 'upload.php', 'admin-ajax.php' ) )
return;
//Editors and Admins can view all media
if( ! current_user_can('delete_pages') )
$wp_query_obj->set('author', $current_user->ID );
return;
}
针对您的特定情况的代码是:
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( ! in_array( $pagenow, array( 'upload.php', 'admin-ajax.php' ) )
return;
//Assuming user1 is the username and 1 is their userID
if( 'user1' !== $current_user->user_login )
$wp_query_obj->set('author__not_in', array(1) );
//Assuming user50 is the username and 50 is their userID
if( 'user50' !== $current_user->user_login )
$wp_query_obj->set('author__not_in', array(50) );
return;
}
【讨论】: