很多WordPress主题都是带有多用户的功能,在WordPress后台会默认显示的所有用户文章、媒体文件,这样的情况会导致大家都可以看到其他用户的文章、媒体文件,很不方便管理。
WordPress后台只显示当前登录用户的文章和媒体文件

有没有办法能够让后台后台只显示当前登录用户的文章和媒体文件呢?

当然是可以的!具体方法是将下面的代码添加到当前主题的 functions.php 文件即可:

//仅显示当前用户的文章、媒体文件

add_action( \'init\', \'check_user_role\' );
function check_user_role() {
    global $current_user;
    if( $current_user->roles[0] != \'administrator\' ) {
        //在[媒体库]只显示用户上传的文件
        add_action(\'pre_get_posts\',\'MBT_restrict_media_library\');
        function MBT_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;
        }
    }
}

注:如果你的WordPress所使用的数据库前缀不是默认的wp,请将第 5 、6 行中的wp 修改为你的WordPress站点的数据库前缀。更多技术教程,请关注技术教程网www.jishujc.com