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."https://wordpress.stackexchange.com/");

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?

3 Answers
3

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'] . "https://wordpress.stackexchange.com/" . $subdir;
        $modified['path'] = $original['basedir'] . DIRECTORY_SEPARATOR . $subdir;
    }
    return $modified;
}
add_filter( 'upload_dir', 'per_user_upload_dir');

Leave a Reply

Your email address will not be published. Required fields are marked *