2

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?

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

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

New contributor
Shohag Malik is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
  • 1
    you should really format your answer... – RiddleMeThis Jan 14 at 17:46

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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