如何在媒体菜单中隐藏其他用户上传的媒体?

时间:2011-10-05 作者:Mr.Brown

简单地说,我如何才能完全隐藏其他用户上传的媒体文件?我想禁用对这些链接的访问(&A);文件,因为它们是非管理员用户访问和编辑自定义帖子类型的一种方式,而自定义帖子类型通常在wordpress后端不可见。

注:我仍然希望他们能够上传和编辑自己的媒体文件,只是为了清楚。

4 个回复
最合适的回答,由SO网友:Paul 整理而成

实际上你可以,下面是如何做到的:

此代码隐藏所有不属于当前登录作者的帖子和媒体(您可以将其更改为应用于其他用户角色)。它还修复了过滤条上的帖子和媒体计数(例如,所有|图像|视频|未附加)。

// Show only posts and media related to logged in author
add_action(\'pre_get_posts\', \'query_set_only_author\' );
function query_set_only_author( $wp_query ) {
    global $current_user;
    if( is_admin() && !current_user_can(\'edit_others_posts\') ) {
        $wp_query->set( \'author\', $current_user->ID );
        add_filter(\'views_edit-post\', \'fix_post_counts\');
        add_filter(\'views_upload\', \'fix_media_counts\');
    }
}

// Fix post counts
function fix_post_counts($views) {
    global $current_user, $wp_query;
    unset($views[\'mine\']);
    $types = array(
        array( \'status\' =>  NULL ),
        array( \'status\' => \'publish\' ),
        array( \'status\' => \'draft\' ),
        array( \'status\' => \'pending\' ),
        array( \'status\' => \'trash\' )
    );
    foreach( $types as $type ) {
        $query = array(
            \'author\'      => $current_user->ID,
            \'post_type\'   => \'post\',
            \'post_status\' => $type[\'status\']
        );
        $result = new WP_Query($query);
        if( $type[\'status\'] == NULL ):
            $class = ($wp_query->query_vars[\'post_status\'] == NULL) ? \' class="current"\' : \'\';
            $views[\'all\'] = sprintf(__(\'<a href="%s"\'. $class .\'>All <span class="count">(%d)</span></a>\', \'all\'),
                admin_url(\'edit.php?post_type=post\'),
                $result->found_posts);
        elseif( $type[\'status\'] == \'publish\' ):
            $class = ($wp_query->query_vars[\'post_status\'] == \'publish\') ? \' class="current"\' : \'\';
            $views[\'publish\'] = sprintf(__(\'<a href="%s"\'. $class .\'>Published <span class="count">(%d)</span></a>\', \'publish\'),
                admin_url(\'edit.php?post_status=publish&post_type=post\'),
                $result->found_posts);
        elseif( $type[\'status\'] == \'draft\' ):
            $class = ($wp_query->query_vars[\'post_status\'] == \'draft\') ? \' class="current"\' : \'\';
            $views[\'draft\'] = sprintf(__(\'<a href="%s"\'. $class .\'>Draft\'. ((sizeof($result->posts) > 1) ? "s" : "") .\' <span class="count">(%d)</span></a>\', \'draft\'),
                admin_url(\'edit.php?post_status=draft&post_type=post\'),
                $result->found_posts);
        elseif( $type[\'status\'] == \'pending\' ):
            $class = ($wp_query->query_vars[\'post_status\'] == \'pending\') ? \' class="current"\' : \'\';
            $views[\'pending\'] = sprintf(__(\'<a href="%s"\'. $class .\'>Pending <span class="count">(%d)</span></a>\', \'pending\'),
                admin_url(\'edit.php?post_status=pending&post_type=post\'),
                $result->found_posts);
        elseif( $type[\'status\'] == \'trash\' ):
            $class = ($wp_query->query_vars[\'post_status\'] == \'trash\') ? \' class="current"\' : \'\';
            $views[\'trash\'] = sprintf(__(\'<a href="%s"\'. $class .\'>Trash <span class="count">(%d)</span></a>\', \'trash\'),
                admin_url(\'edit.php?post_status=trash&post_type=post\'),
                $result->found_posts);
        endif;
    }
    return $views;
}

// Fix media counts
function fix_media_counts($views) {
    $_total_posts = array();
    $_num_posts = array();
    global $wpdb, $current_user, $post_mime_types, $avail_post_mime_types;
    $views = array();
    $count = $wpdb->get_results( "
        SELECT post_mime_type, COUNT( * ) AS num_posts 
        FROM $wpdb->posts 
        WHERE post_type = \'attachment\' 
        AND post_author = $current_user->ID 
        AND post_status != \'trash\' 
        GROUP BY post_mime_type
    ", ARRAY_A );
    foreach( $count as $row )
        $_num_posts[$row[\'post_mime_type\']] = $row[\'num_posts\'];
    $_total_posts = array_sum($_num_posts);
    $detached = isset( $_REQUEST[\'detached\'] ) || isset( $_REQUEST[\'find_detached\'] );
    if ( !isset( $total_orphans ) )
        $total_orphans = $wpdb->get_var("
            SELECT COUNT( * ) 
            FROM $wpdb->posts 
            WHERE post_type = \'attachment\' 
            AND post_author = $current_user->ID 
            AND post_status != \'trash\' 
            AND post_parent < 1
        ");
    $matches = wp_match_mime_types(array_keys($post_mime_types), array_keys($_num_posts));
    foreach ( $matches as $type => $reals )
        foreach ( $reals as $real )
            $num_posts[$type] = ( isset( $num_posts[$type] ) ) ? $num_posts[$type] + $_num_posts[$real] : $_num_posts[$real];
    $class = ( empty($_GET[\'post_mime_type\']) && !$detached && !isset($_GET[\'status\']) ) ? \' class="current"\' : \'\';
    $views[\'all\'] = "<a href=\'upload.php\'$class>" . sprintf( __(\'All <span class="count">(%s)</span>\', \'uploaded files\' ), number_format_i18n( $_total_posts )) . \'</a>\';
    foreach ( $post_mime_types as $mime_type => $label ) {
        $class = \'\';
        if ( !wp_match_mime_types($mime_type, $avail_post_mime_types) )
            continue;
        if ( !empty($_GET[\'post_mime_type\']) && wp_match_mime_types($mime_type, $_GET[\'post_mime_type\']) )
            $class = \' class="current"\';
        if ( !empty( $num_posts[$mime_type] ) )
            $views[$mime_type] = "<a href=\'upload.php?post_mime_type=$mime_type\'$class>" . sprintf( translate_nooped_plural( $label[2], $num_posts[$mime_type] ), $num_posts[$mime_type] ) . \'</a>\';
    }
    $views[\'detached\'] = \'<a href="upload.php?detached=1"\' . ( $detached ? \' class="current"\' : \'\' ) . \'>\' . sprintf( __( \'Unattached <span class="count">(%s)</span>\', \'detached files\' ), $total_orphans ) . \'</a>\';
    return $views;
}

SO网友:Maciej Gurban

要使Paul的解决方案与ACF配合使用,只需将第一个函数更改为:

function current_author_posts( $wp_query ) {

if ( strpos( admin_url(), \'/edit.php\' ) !== false ) {
    if ( !current_user_can( \'delete_pages\' ) ) {
        global $current_user;
        $wp_query->set( \'author\', $current_user->ID );
        add_filter(\'views_edit-post\', \'fix_post_counts\');
        add_filter(\'views_upload\', \'fix_media_counts\');
    }
}
}
add_filter(\'parse_query\', \'current_author_posts\' );
供参考,see ACF Support forum 还有那里的最新帖子

SO网友:Jeffrey Carandang

这个函数更容易添加到函数中。php和类似charm的工作方式:

function hide_media_by_other($query) {
global $pagenow;

if( \'upload.php\' != $pagenow || !$query->is_admin ){
    return $query;
}

if( !current_user_can( \'manage_options\' ) ) {
    global $user_ID;
    $query->set(\'author\', $user_ID );
}
return $query;
    }
    add_filter(\'pre_get_posts\', \'hide_media_by_other\');
要查看完整教程,包括通过单击“添加媒体”按钮隐藏媒体iframe上的图像,请按照以下步骤操作:http://jeffreycarandang.com/tutorials/hide-wordpress-posts-media-uploaded-users/

干杯,笨蛋

SO网友:Wyck

老实说,WordPress现在是如何工作的,你不能。你必须编写自己的PHP来实现这一点。

在如何处理用户和媒体方面,它确实需要做大量的工作。

结束

相关推荐

如何从WordPress主导航中删除整个Media部分,而不只是隐藏它?

我想从左侧的WordPress管理导航中删除整个“媒体”部分。我不想做的只是用CSS或下面的插件/函数“隐藏”它,因为它仍然可以通过URL访问。这一点至关重要,用户不应以任何方式看到彼此上传。这是我目前拥有的,只是隐藏了它。如果你去上传。在url中,您仍然可以访问它。add_action( \'admin_menu\', \'remove_menu_links\' ); function remove_menu_links() { remove_menu_page(\'upload