How to change upload directory based on frontend form input name or ID?

I created a frontend form using a plugin which allows logged in users to create draft posts. There are 3 file upload areas. I need 1 of them to use the default public WP media upload folder as usual. But I need the files uploaded via 2nd and 3rd file upload areas to move to a restricted sub folder inside the default WordPress file upload folder. 1 of them allow multiple file uploads.

Important facts:

  1. I have changed the default WP_CONTENT folder and UPLOADS folder using wp-config.php
  2. One of the file upload areas allows multiple file uploads

This is the file upload field generated by a custom plugin:
<input type="text" class="multiple_files" data-formid="11" data-fieldname="multiple_files" placeholder="http://" name="files[0]" value="" data-required="yes" data-type="multiple" required="" autocomplete="off">

People can choose to add more, then the name changes to files[1], files[2] , so on..

What I basically need is something like this:

add_filter('wp_handle_upload_prefilter', 'custom_upload_prefilter');
add_filter('wp_handle_upload', 'custom_upload_handle');

function custom_upload_prefilter($file){
    add_filter('upload_dir', 'custom_upload_dir');
    return $file;
}

function custom_upload_handle($fileinfo){
    remove_filter('upload_dir', 'custom_upload_dir');
    return $fileinfo;
}

function custom_upload_dir($path){  
if ( isset( $_REQUEST['files'][0] ) ) {
    $customdir="/secured". $path['subdir'];     
        $path['path']   = $path['basedir'] . $customdir;
        $path['url']      = $path['baseurl'] . $customdir;
}
return $path;
}

But $_REQUEST only works when you have submitted the form, not when you upload the files.

I’m new to WordPress. I looked around to find an exact solution to my issue. there are a few solution but I not exactly what I’m looking for. Thank you very much.

0

Leave a Comment