Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wordpress show only media user has uploaded in wp_editor

I'm creating a wordpress site where the registered user has the ability to create his own post via wp_editor() on the frontend, but just one post.

Now I want to restrict the user to be able to only see his uploaded media. I use the following script in the functions.php, which works in the backend. So if a user goes to the media section in the backend he will only see his uploaded media.

But if the user goes to "insert media" pop-up on the frontend wp_editor he can still see the uploaded media from all the users.

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' );

Do you have any idea hot to solve this annoyance? Thank you!

like image 450
Sebsemillia Avatar asked Mar 15 '13 13:03

Sebsemillia


1 Answers

You might try this plugin: http://wordpress.org/extend/plugins/view-own-posts-media-only/

Alternatively try this:

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

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

like image 74
Joe Spurling Avatar answered Nov 09 '22 13:11

Joe Spurling