我的网站中有200个用户,当从wp-admin->上传时,我需要隐藏所有其他用户的user1
所有图片上传内容。媒体上传。
如果user50
上传了新图片,则只有他才能在媒体上传中看到它。
怎么做?
答案 0 :(得分:2)
1)您可以使用此插件 仅查看自己的帖子媒体(https://wordpress.org/plugins/view-own-posts-media-only/)
2)按代码限制 将此代码段添加到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,媒体库)
答案 1 :(得分:1)
我一直在寻找这个,我也遇到了一个过时的,这对我有用,只需将它添加到你的功能中:
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;
}
答案 2 :(得分:0)
使用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;
}