WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want display and limit disk usage for each author and be able to manage disk usage as an administrator.

Example: author1: disk space is 20Mb; if they uploaded more 20Mb the upload button (in media library and post) is disabled.

author2: disk space is 30Mb, if they upload more 30Mb the upload button (in media library and post) is disabled.

share|improve this question

Okey so this is just an example how you can archive this..

First change the upload directory to the current users username. eg uploads/admin

function wpse_16722_type_upload_dir( $args ) {
    // Get current user data
    $current_user = wp_get_current_user();

    // Make upload dir to current username
    $newdir = '/' . $current_user->user_login;

    $args['path']    = str_replace( $args['subdir'], '', $args['path'] ); //remove default subdir
    $args['url']     = str_replace( $args['subdir'], '', $args['url'] );      
    $args['subdir']  = $newdir;
    $args['path']   .= $newdir; 
    $args['url']    .= $newdir; 

    return $args;
add_filter( 'upload_dir', 'wpse_16722_type_upload_dir' );

Then we need a function to check the size of the current users uploads. I found a function called recurse_dirsize within ms-functions.php that calculate the directory in bytes.

 * Get the size of a directory recursively.
 * Used by get_dirsize() to get a directory's size when it contains
 * other directories.
 * @since MU
 * @param string $directory
 * @return int
function recurse_dirsize( $directory ) {
    $size = 0;

    $directory = untrailingslashit( $directory );

    if ( !file_exists($directory) || !is_dir( $directory ) || !is_readable( $directory ) )
        return false;

    if ($handle = opendir($directory)) {
        while(($file = readdir($handle)) !== false) {
            $path = $directory.'/'.$file;
            if ($file != '.' && $file != '..') {
                if (is_file($path)) {
                    $size += filesize($path);
                } elseif (is_dir($path)) {
                    $handlesize = recurse_dirsize($path);
                    if ($handlesize > 0)
                        $size += $handlesize;
    return $size;

And then we need to check if the current users uploads is bigger than x if so disable media_buttons

function wpse_16722_user_upload_size() {
    // Get current user data
    $current_user = wp_get_current_user();

    // Array of key => value pairs
    $uploads = wp_upload_dir(); 

    $dir = $uploads['basedir'] . '/' .  $current_user->user_login;

    if( file_exists( $dir ) && is_dir( $dir ) ) {
        $size = recurse_dirsize( $dir ) / 1024 / 1024;

        // Convert bytes to human readable format
        $new_size = round( $size / 1024 * 1024, 2 );

        // If current users upload dir is
        // Bigger than 20 MB disable
        // Upload buttton
        if( $new_size  >= 20 ) {
            remove_action( 'media_buttons', 'media_buttons' );

add_action('admin_head', 'wpse_16722_user_upload_size');

You also wanted to control more than one user and different sizes. I would recommend you to add a settings-page that saves the users available size with update_user_meta($user_id, 'size_limit', $value ); Read more about that here

When you have the sizes added to the users_meta you can change the size within the function wpse_16722_user_upload_size "20" to the size within get_user_meta( $user_id, 'size_limit');

share|improve this answer
Great solution with uploads/username. Applaud. – Maxim Orlovsky Dec 23 '12 at 19:03
// this function disable media buttons
function removemediabuttons()
   remove_action( 'media_buttons', 'media_buttons' );

// this function check the total size of all attachments by user. 
//But it doesnt count sizes of thumbnails. 
// $limitBytes -- maximum size in bytes
function checkMidiaSizeByUser($userId, $limitBytes) 
    // get all attachments of $userId
    $attachements = query_posts(
        'post_type' => 'attachment',
        'author' => $userId
    // count size
    $size = 0;
    foreach($attachements as $attachment){
      $size += filesize( get_attached_file( $attachment->ID ) );
    // check limit
    if ( $size >= $limitBytes ) {

/* **************************************** */

// Set our limit to 20Mb
$limit = 20*1024*1024; 

// get current user
global $current_user;

//check limit and disable media buttons if user met limit
checkMidiaSizeByUser($current_user->ID, $limit);

I don't test this code in real work, but it should work.

share|improve this answer
although I have not tested, but I think it runs fine, however I would like to thank you very much for answering my question.thank you very much – vt7211 Dec 24 '12 at 2:57
It would be nice if you select the best answer and "solve" your question in this way. – Maxim Orlovsky Dec 24 '12 at 4:42
oh, it run, but submenu media libary ( add media ) don't diasbale. and it can't manager by user. because more users, i want disk space differant and easy manager ( change disk space each user ),i think manager by list user by page .about one list user and disk space corresponding. i look for. I look forward to receiving feedback from you soon – vt7211 Dec 24 '12 at 12:43
i saw errors, the home page don't show pages & posts true and website run not true. wp admin can't show list post & page. – vt7211 Dec 24 '12 at 17:08
i tried fix errors, i add code <?php wp_reset_query(); ?> and fixed. but i can't manager disk space each user.i think manager in profile user. help me – vt7211 Dec 25 '12 at 15:15

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.