Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

we have several users on our site. we have created a separate folder for each user. Admin have a select box to select users on the site. we have to give ability to upload files to that particular user on selection.

I have created a hook to upload_dir. its working fine with the logged in user. but how to make it dynamic on admin panel. How make this work when admin selected an user in drop down menu and upload files to that user he selected.

    function custom_upload_directory( $args ) {
        global $current_user;
        get_currentuserinfo();
        $base_directory = ABSPATH."user_documents/".$current_user->user_login;
        $base_url = home_url()."/user_documents/".$current_user->user_login;
        $args['path'] = $base_directory;
        $args['url']  = $base_url;
        $args['subdir'] = $current_user->user_login;
        $args['basedir'] = $base_directory;
        $args['baseurl'] = $base_url;return $args;
    }
    add_filter( 'upload_dir', 'custom_upload_directory' );

admin screen

upon selecting a user from drop down i have to upload a file to his folder.

dropdown code

<div id="userlistdiv" >
    <form method="post" action="">
        <h2><?php echo "users: " ?></h2>
        <select id="userselection" name="userselection">
        <?php
            $blogusers = get_users('blog_id=1&orderby=login&role=subscriber');
            echo '<option value="" selected="selected"></option>';
            foreach ($blogusers as $user) {
                echo '<option>' . $user->user_login . '</option>';
            }
        ?>
        </select>
        <INPUT TYPE="submit" name="submit" />
    </form>
</div>
share|improve this question
    
Can you share the code you use for your dropdown solution. –  ialocin Dec 9 '13 at 14:05
    
@criticalboot Always add code to questions with an edit. Never in a comment as this is unreadable. Thanks. –  kaiser Dec 9 '13 at 14:54
    
Thanks for help Kaiser... –  criticalboot Dec 9 '13 at 17:15

1 Answer 1

up vote 0 down vote accepted

You can grab a WP_User instance with get_user_by() - Codex documents that pretty well.

add_filter( 'upload_dir', 'userLoginUploadDir' );
function userLoginUploadDir( $args )
{
    // Assuming that your forms user login (search? autocomplete? AJAX-ified? foo?) is named
    // name="user_login" id="user_login"
    $userLogin = esc_attr( $_GET['user_login'] );
    $user = get_user_by( 'login', $userLogin );

    $upload_dir = wp_upload_dir();

    return array_merge( $upload_dir, array(
        # 'path'    => '',
        # 'url'     => '',
        'subdir'  => $user->user_login,
        # 'basedir' => '',
        # 'baseurl' => '',
    ) );
}

Basically it should be enough to only set the specific subdir key. The rest should be available from wp_upload_dir(). In any case: Don't use constants for pathes or URLs/URIs. Use the functions as those are applying the needed filters.

share|improve this answer
    
for user it is working... as a admin i have a dropdown list of users. upon selecting a user from drop down i have to upload a document to that user... means i have to update subdir of upload_dir dynamically. –  criticalboot Dec 9 '13 at 14:36
    
Please read the code in my answer in detail: It does all that. You just have to adjust it to retrieving the user_login value from your dropdown. And even for that an example/placeholder is given. –  kaiser Dec 9 '13 at 14:54
    
Hi Kaiser... I updated you code in line $userLogin = esc_attr( $_POST['userselection'] ); i am getting null in userLogin. –  criticalboot Dec 9 '13 at 16:48
    
Do you get anything for $_POST. Btw, from here on it's quite off-topic and plain HTML form send and PHP retrieval. Please read about that on SO before we proceed. –  kaiser Dec 9 '13 at 17:00

Your Answer

 
discard

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.