Changing uploads folder by custom post type with W3 Total Cache
-
I use W3 Total Cache to sync my CDN (Amazon). It works as intended, except that I am trying to change the uploads folder based on custom post types.
I found the below code. It works perfect for uploading to my local server, into the correct CPT folder, but W3 is trying to find the image in the root directory (ex: wp-content/uploads/450746973-380×214.jpg). It isn’t looking for it in the subfolder where the image was actually uploaded to, so I am getting a “Source file not found.” error. It worked for the old years/months sub-directories, but not the new CPT method.
I have tried adding each specific CPT directory to the custom files list, but that didn’t solve it.
//Upload directory based on custom post type CPT add_filter('upload_dir', 'cgg_upload_dir'); function cgg_upload_dir($dir) { // xxx Lots of $_REQUEST usage in here, not a great idea. // Are we where we want to be? if (!isset($_REQUEST['action']) || 'upload-attachment' !== $_REQUEST['action']) { return $dir; } // make sure we have a post ID if (!isset($_REQUEST['post_id'])) { return $dir; } // modify the path and url. $type = get_post_type($_REQUEST['post_id']); $uploads = apply_filters("{$type}_upload_directory", $type); $dir['path'] = path_join($dir['basedir'], $uploads); $dir['url'] = path_join($dir['baseurl'], $uploads); return $dir; }
The topic ‘Changing uploads folder by custom post type with W3 Total Cache’ is closed to new replies.