【问题标题】:wordpress show only media user has uploaded in wp_editorwordpress 仅显示已在 wp_editor 中上传的媒体用户
【发布时间】:2013-03-04 06:41:37
【问题描述】:

我正在创建一个 wordpress 网站,注册用户可以通过前端的 wp_editor() 创建自己的帖子,但只有一个帖子。

现在我想限制用户只能看到他上传的媒体。我在functions.php中使用以下脚本,它在后端工作。因此,如果用户转到后端的媒体部分,他只会看到他上传的媒体。

但是如果用户去前端 wp_editor 上的“插入媒体”弹出窗口,他仍然可以看到所有用户上传的媒体。

function restricted_media_view( $wp_query ) {
if ( strpos( $_SERVER[ 'REQUEST_URI' ], '/wp-admin/upload.php' ) !== false  
|| strpos( $_SERVER[ 'REQUEST_URI' ], '/wp-admin/edit.php' ) !== false ) {
    if ( !current_user_can( 'level_5' ) ) {
        global $current_user;
        $wp_query->set( 'author', $current_user->id );
    }
    }
}
add_filter('parse_query', 'restricted_media_view' );

你有什么想法可以解决这个烦恼吗?谢谢!

【问题讨论】:

    标签: wordpress view media restrictions


    【解决方案1】:

    你可以试试这个插件:http://wordpress.org/extend/plugins/view-own-posts-media-only/

    或者试试这个:

    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;
    }
    

    来源:http://wpsnipp.com/index.php/functions-php/restricting-users-to-view-only-media-library-items-they-upload/#comment-810649773

    【讨论】:

    • 谢谢,这真的很难找到!
    【解决方案2】:

    或者自从WordPress 3.7

    add_filter( 'ajax_query_attachments_args', "user_restrict_media_library" );
    function user_restrict_media_library(  $query ) {
        global $current_user;
        $query['author'] = $current_user->ID ;
        return $query;
    }
    

    【讨论】:

    【解决方案3】:

    我使用 WP 4.3.1 的 API/过滤器参考/ajax 查询附件 args 并且工作

    add_filter( 'ajax_query_attachments_args', 'show_current_user_attachments', 10, 1 );
    
    function show_current_user_attachments( $query = array() ) {
        $user_id = get_current_user_id();
        if( $user_id ) {
            $query['author'] = $user_id;
        }
        return $query;
    }
    

    只需添加functions.php

    或查看此链接WP Codex

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-25
      • 2015-07-01
      • 2019-01-21
      • 1970-01-01
      • 1970-01-01
      • 2017-12-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多