Lot of users request the ability to customize the upload path of WordPress File Upload plugin.
This article describes how we can change the upload path according to our needs. In the following example we have selected the uploaded files to be added in Media folder of our website (medialink attribute set to “true”). When medialink is true, the plugin ignores the path set in uploadpath attribute and uses the default upload folder of WordPress. We will change the upload path to be inside the default upload folder, however files will go to subdirectories based on the username of the user who makes the upload.
To do that we need to install Code Snippets, a 3rd party plugin to assist us implement some filters and actions of WordPress File Upload plugin and also assign to the plugin specific settings. Here are the steps:
1. Install Code Snippets
2. Go to your Dashboard, in Snippets Tab and create a new code snippet.
3. Give it a name, whatever you like. Paste the following code inside Code textbox of your snippet.
if (!function_exists('wfu_before_file_upload_handler')) { function wfu_before_file_upload_handler($file_path, $file_unique_id) { //get the current user $user = wp_get_current_user(); if ( $user->ID != 0 ) { //extract filename from $file_path $filename = wfu_basename($file_path); //get upload dir structure $upload_dir = wp_upload_dir(); //construct new upload dir from upload base dir and the username of the current user $new_file_dir = $upload_dir['basedir'].'/'.$user->user_login; //create the new file dir, if it is does not exist if ( !is_dir($new_file_dir) ) mkdir($new_file_dir, 0777, true); //return the new file path return $new_file_dir.'/'.$filename; } } add_filter('wfu_before_file_upload', 'wfu_before_file_upload_handler', 10, 2); }
4. Leave all other options are they are and press “Save Changes and Activate”.
5. In WordPress File Upload plugin and using the Shortcode Composer go to Interoperability Tab and enable “Add Uploaded Files To Media”. Alternatively, you can just add the following code inside your shortcode: medialink=”true”.
6. We are ready to go. When a logged user uploads a file (image), the file will be added to the Media folder of your website. The upload path of the file will be “/wp-content/uploads/%username%” where %username% is the username of the logged user.
This is just a small example of what can be done using the filters and actions of the plugin. You can customize the above code, to generate your own upload directories. You can even modify the name of the uploaded file.
It is noted that the above code is executed after all the native checks of the plugin. This means that if the upload directory or the filename are invalid, the plugin will not be able to detect it (it will just reject the upload without specific error). You need to implement your own checks inside the code, to make sure that the file path is valid.
For any questions or asiistance please contact the Iptanus team.
test
Hi, I am trying to upload a file to http://koditv.uk/kodidl folder.
This is the code [wordpress_file_upload multiple=”false” uploadpath=”http://koditv.uk/kodidl” maxsize=”1000″ showtargetfolder=”true” subfoldertree=”auto”]
Cant get it to work.
Do you have any sugestions?
I still dont get any output to the screen if I use this method and add an echo at the end of the snippet.
I am trying to use the hook that checks if the upload was successful so that my own plugin can go ahead and parse the uploaded file but this doesnt seem to work at all.
im lost please help.
Thanks Chris
How would I create a directory based on user input – like if I had a Name field as part of the upload, how do I create a directory named after that name (as opposed to the wp user ID)?
Also (and I suspect this one is trickier): is there a way to assign a new directory if you’re using Dropbox uploads?
Hi, if you set uploadpath=”/uploads/%userdata1%” then %userdata1% variable will be replaced by the value entered by the user in the first field. If you use %userdata2% the second and so on.
Regarding Dropbox folders, if the folder does not exist then it will be created automatically.
Regards
Nickolas
file uploaded but my file location on /uploads/year/month/
how to fix it?
My Shortcode
[wordpress_file_upload uploadpath=”uploads/%userdata2%/%userdata3%/%userdata1%/%userdata4%/”]
Do you have the Free or Pro version?
Regards
Nickolas
This worked like a charm! Thank you! So glad I got the Pro version too.
Great working fine and easy 🙂
for the Snippets you can Also use Woody snippets