2013-03-15 56 views
6

我創建其中註冊用戶必須創建自己通過在前端wp_editor()後的能力的WordPress站點,但只是一個職位。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'); 

你有什麼想法解決這個煩惱嗎?謝謝!

回答

11

你可以試試這個插件: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

+0

謝謝!!插件似乎爲我做了詭計! :) – Sebsemillia 2013-03-15 14:31:33

+0

太好了。很高興它的工作。 – 2013-03-15 14:31:57

+0

謝謝,這真的很難嘗試找到! – 2014-02-14 02:59:41

6

或者因爲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; 
} 
+1

這也適用於我的3.8,謝謝:) – 2014-02-14 03:05:04

+1

文檔中提供了一個稍微不同的方法:http://codex.wordpress.org/Plugin_API/Filter_Reference/ajax_query_attachments_args#Examples – David 2015-02-21 00:51:59

1

我用API/Filter參考手冊/ ajax查詢附件已廢除了WP 4.3.1指定參數和工作

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

相關問題