2

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.

6
  • 1
    Uploads in WP create a post of type attachment, WordPress itself doesn't care about the location of the files, and there's no built in capability to restrict access to those folders as they're served directly by Apache/Nginx, so to talk about multiple locations doesn't make much sense. Since this isn't a discussion forum, and people need to be able to write a specific answer that fully answers your question, what kind of answer are you looking for? I notice there is no code in your question to work with, can you edit your Q to include it? What problem does this solve?
    – Tom J Nowell
    Commented Nov 18, 2020 at 20:56
  • Thanks for the comment @TomJNowell Basically something similar to this: answer But instead of the file type, I need to filter based on input name or the id of a form.
    – user197754
    Commented Nov 19, 2020 at 3:10
  • Since you are saying that the upload is handled by a custom plugin, it means you will need to check if that plugin can be customized with hooks. You will have to look in the custom plugin code to see where the actual file saving is happening to see if the process can be modified.
    – Olivier
    Commented Dec 1, 2020 at 17:16
  • @Olivier unfortunately there's none. I'm not sticking to a plugin either. I could use the WordPress hooks, just don't know how to filter this functionality based on input value.
    – user197754
    Commented Dec 2, 2020 at 4:31
  • @user197754, if $_REQUEST doesn't contain the file data, then try with $_FILES, but as @Olivier said, you need to know the code (JS/PHP) used for uploading the file / processing it on the server. But are you sure $_REQUEST doesn't actually contain any data specific to the uploaded file (or upload form) - try var_dump( $_REQUEST, $_FILES )?
    – Sally CJ
    Commented Dec 3, 2020 at 8:55

0

Your Answer

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

Browse other questions tagged or ask your own question.