WrdoPress 教程 ·

WordPress 多媒体库只显示当前用户上传的文件

WordPress 的 Media Libaray(多媒体库) 里只显示当前用户上传的文件,在当前主题的 functions.php 文件添加下面的代码:

is_a() 函数已废弃,自 PHP 5 起使用 instanceof 类型运算符,上例在 PHP 5 中会是这样

//wordpress show only media user has uploaded 
//在文章编辑页面的[添加媒体]只显示用户自己上传的文件
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') ) 
    if ($current_user instanceof 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; 
}
/在[媒体库]只显示用户上传的文件
function ml_upload_media_library( $wp_query ) {
    if ( strpos( $_SERVER[ 'REQUEST_URI' ], '/wp-admin/upload.php' ) !== false ) {
        if ( !current_user_can( 'manage_options' ) && !current_user_can( 'manage_media_library' ) ) {
            global $current_user;
            $wp_query->set( 'author', $current_user->id );
        }
    }
}
add_filter('parse_query', 'ml_upload_media_library' );

最终效果

参与评论