Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialTom Beech
48 PointsCustom location for pdf file uploads
In wordpress I wanted all images to be uploaded to /images directory and all files (pdf etc) to be uploaded to /files directory, so that they matched the directory structure from a previous CMS I was moving to wordpress.
So I added:
define( 'UPLOADS', 'images');
to the wordpress config.php file, now all files get uploaded to /images and not the default /uploads directory in wordpress. This is regarless of whether they are a image or pdf file.
I then used the below code in my themes functions.php file.
The below code detects when a pdf file is being uploaded, however it puts it in a subdirectory of the /images directory I declared in the wordpress config.php file using - define( 'UPLOADS', 'images');
How would I adjust the below code to make the pdf file be uploaded to /files directory and not /images/files as is the case at the moment.
Any help would be appreciated.
<?php
/*
* Change upload directory for PDF files
* Only works in WordPress 3.3+
*/
add_filter('wp_handle_upload_prefilter', 'wpse47415_pre_upload');
add_filter('wp_handle_upload', 'wpse47415_post_upload');
function wpse47415_pre_upload($file){
add_filter('upload_dir', 'wpse47415_custom_upload_dir');
return $file;
}
function wpse47415_post_upload($fileinfo){
remove_filter('upload_dir', 'wpse47415_custom_upload_dir');
return $fileinfo;
}
function wpse47415_custom_upload_dir($path){
$extension = substr(strrchr($_POST['name'],'.'),1);
if(!empty($path['error']) || $extension != 'pdf') { return $path; } //error or other filetype; do nothing.
$customdir = '/files';
$path['path'] = str_replace($path['subdir'], '', $path['path']); //remove default subdir (year/month)
$path['url'] = str_replace($path['subdir'], '', $path['url']);
$path['subdir'] = $customdir;
$path['path'] .= $customdir;
$path['url'] .= $customdir;
return $path;
}
2 Answers
Tom Beech
48 PointsThanks Joel
If I change $customdir to '../files'
the path in the wordpress admin area file url is outputted as /images/../files/pdf-file-name.pdf
Joel Bardsley
31,249 PointsOf course, what a wally I am!
I'm probably overcomplicating it, but you could try using the dirname function to set $path['path'] up a level before appending the $customdir.
Tom Beech
48 PointsHi Joel
I've been struggling with this for a few days now and not getting anywhere.
Is there any chance you could give an example of the code for the above solution you suggested?
Thanks
Joel Bardsley
31,249 PointsJoel Bardsley
31,249 PointsWhat happens if you change the value of $customdir to '../files' ?