WordPress.org

Ready to get started?Download WordPress

Forums

Restricting Users to View Only their own Media Library Items (7 posts)

  1. aw2
    Member
    Posted 3 years ago #

    I want users to be able to upload photos using 'upload_files' but in their profile page, the Media Library shows every image that's been uploaded by every user. How can I filter that so that they can only view the images THEY uploaded themselves?

  2. bruha
    Member
    Posted 3 years ago #

    <?php
    /*
    Plugin Name: Manage Your Media Only
    Version: 0.1
    */
    
    //Manage Your Media Only
    function mymo_parse_query_useronly( $wp_query ) {
        if ( strpos( $_SERVER[ 'REQUEST_URI' ], '/wp-admin/upload.php' ) !== false ) {
            if ( !current_user_can( 'level_5' ) ) {
                global $current_user;
                $wp_query->set( 'author', $current_user->id );
            }
        }
    }
    
    add_filter('parse_query', 'mymo_parse_query_useronly' );
    ?>

    Save code above as manage_your_media_only.php, zip it, upload as plugin to your WP and activate it, that's all.

  3. aw2
    Member
    Posted 3 years ago #

    Hi Bruha,

    Thank you very much for your reply.

    I've tried your plugin, but I get 2 problems:
    1) the plugin-code is visible on the top of my website
    2) my admin is gone. If I go to http://www.mydomain.com/wp-admin/ looks like this on my screen:

    _______________

    <?php
    /*
    Plugin Name: Manage Your Media Only
    Version: 0.1
    */
    
    //Manage Your Media Only
    function mymo_parse_query_useronly( $wp_query ) {
        if ( strpos( $_SERVER[ 'REQUEST_URI' ], '/wp-admin/upload.php' ) !== false ) {
            if ( !current_user_can( 'level_5' ) ) {
                global $current_user;
                $wp_query->set( 'author', $current_user->id );
            }
        }
    }
    
    add_filter('parse_query', 'mymo_parse_query_useronly' );
    ?>
    
    Warning: Cannot modify header information - headers already sent by (output started at .../wp-content/plugins/manage_your_media_only.php:9) in .../public_html/wp-includes/pluggable.php on line 890

    _______________

    I deactivated all other plugins.
    Am I doing something wrong.
    I really appreciate your help.

    Thank you very much in advance!
    Arthur

  4. bruha
    Member
    Posted 3 years ago #

    i made it for your and check it, try this manage_your_media_only.zip

  5. billimebilly
    Member
    Posted 3 years ago #

    Hi bruha! Thank you, I was looking for this!

    But when I use WordPress featured images, the Media Gallery tab still shows all Media Uploaded from all users, thus I can edit them too (like changing caption of an image)..

    any ideas?

  6. sirtimo
    Member
    Posted 3 years ago #

    Hello everyone,

    I managed to install this as well, since i'm here with the same problem, but the link 'unattached images' in the media library still shows all images by everyone. Thats a bit odd! Any idea on how to fix that? can't find anything i'm looking for an hour already.

    Thanks

  7. flamingalah
    Member
    Posted 2 years ago #

    bruha - I used your code and it seems to work perfectly, thanks.

Topic Closed

This topic has been closed to new replies.

About this Topic