How To Restricting Users To View Only Media Library Items They Upload In WordPress

By default, WordPress allows every author to view all the images in media library, which isn’t a great idea, right? Adding this snippet to WordPress’ current theme’s functions.php file will restrict users to view only media library items they uploaded in the library.

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

If you found this post useful, share it with your friends:

7 thoughts on “How To Restricting Users To View Only Media Library Items They Upload In WordPress”

      1. I think, this will do the job…


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

Leave a Reply