3

I set the content and the wordpress directory of wordpress like described in the codex:

define('WP_SITEURL', 'https://' . $_SERVER['SERVER_NAME'] . '/wordpress');
define('WP_CONTENT_DIR', dirname(__FILE__) . '/content');

Now what I want to do is set an individual upload directory for every user, like this:

wpinstance.org/content/uploads/user-name/here-goes-the-file.jpg

I already tried a lot using

define( 'UPLOADS', dirname(__FILE__) . '/content/uploads'.$current_user->user_name.'/');

as well as many combinations without dirname() and so on. It all turned out to take the files up to wordpress/…something and leaving the user name part empty. So how can I achieve this? Any ideas?

2
  • I think you need this: $current_user->user_login. Username is not an accepted value that I know of for WordPress core.
    – Aibrean
    Commented Jan 23, 2015 at 16:21
  • nope, sorry. that returns an empty string as well :(
    – mcnesium
    Commented Jan 24, 2015 at 11:10

4 Answers 4

6

With credits to petermolnar via irc://freenode.net/wordpress I can answer my own question. The key is to set an upload-dir filter in the theme's functions.php:

function per_user_upload_dir( $original ){
    // use the original array for initial setup
    $modified = $original;
    // set our own replacements
    if ( is_user_logged_in() ) {
        $current_user = wp_get_current_user();
        $subdir = $current_user->user_login;
        $modified['subdir'] = $subdir;
        $modified['url'] = $original['baseurl'] . '/' . $subdir;
        $modified['path'] = $original['basedir'] . DIRECTORY_SEPARATOR . $subdir;
    }
    return $modified;
}
add_filter( 'upload_dir', 'per_user_upload_dir');
0
function per_user_upload_dir( $original ){
    // use the original array for initial setup
    $modified = $original;
    // set our own replacements
    if ( is_user_logged_in() ) {
        $current_user = wp_get_current_user();
        $subdir = $current_user->user_login;
        $modified['subdir'] = $subdir;
        $modified['url'] = $original['baseurl'] . '/' . $subdir;
        $modified['path'] = $original['basedir'] . DIRECTORY_SEPARATOR . $subdir;
    }
    return $modified;
}
add_filter( 'upload_dir', 'per_user_upload_dir');

it's worked for me... but is it possible to make another subfolder for all users, like uploads/users/ ? i want to keep all users content into users folder under the upload directory

0
0

Nice solution. The uploads folder might get a bit cluttered if many users are uploading. Easy fix is to change:

$subdir = $current_user->user_login;

to

$subdir = 'user_docs/'.$current_user->user_login;

The modified example below also only does this for a specific role (instructor)

function per_user_upload_dir( $original ){
  $modified = $original;
  if ( is_user_logged_in() && current_user_can('instructor') ) {
      $current_user = wp_get_current_user();
      $subdir = 'user_docs/'.$current_user->user_login;
      $modified['subdir'] = $subdir;
      $modified['url'] = $original['baseurl'] . '/' . $subdir;
      $modified['path'] = $original['basedir'] . DIRECTORY_SEPARATOR . $subdir;
  }
  return $modified;
}
add_filter( 'upload_dir', 'per_user_upload_dir');
0

on one of my sites this code works perfectly however on another it displays this error on the home page:

Warning: call_user_func_array() expects parameter 1 to be a valid callback, function 'per_user_upload_dir' not found or invalid function name in /home/users/xxx/public_html/xxx/wp-includes/class-wp-hook.php on line

What could be the cause? The template used is: Jaxon

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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