I’m using a snippet of code in my functions.php which should in theory change the location of my uploads for my custom post type (leaving the pages and posts upload directory the same)
My custom post type is ‘download‘
and my new directory is a folder called ‘downloads‘ in my ‘wp-content‘ folder.
My wp-content directory looks like this…
- wp-content
- downloads
- plugins
- themes
- uploads
See function below, can any help me understand why this is not working? Thanks.
add_filter( 'upload_dir', 'my_custom_upload_dir' );
function my_custom_upload_dir( $default_dir ){
global $post;
if ( $post->post_type != 'download' ) {
return $default_dir;
}
/*
* On success, the returned array will have many indices:
* 'path' - base directory and sub directory or full path to upload directory.
* 'url' - base url and sub directory or absolute URL to upload directory.
* 'subdir' - sub directory if uploads use year/month folders option is on.
* 'basedir' - path without subdir.
* 'baseurl' - URL path without subdir.
* 'error' - set to false.
*/
// Adjust settings here
$bdir = 'wp-content';
$subdir = date( 'Y/m' );
$dir = $bdir . $subdir;
$burl = content_url('downloads');
$url = $burl . $subdir;
$custom_dir = array(
'path' => $dir,
'url' => $url,
'subdir' => $subdir,
'basedir' => $bdir,
'baseurl' => $burl
);
return shortcode_atts( $custom_dir, $default_dir );
}
The
uploads_dir
filter needs another implementation to successfully modifying the uploads dir/path.The caveat here is that the structure is
/wp-content/uploads/downloads/yyyy/mm/filename.ext
.I thought the following would handle
/wp-content/downloads/...
but am not being able to determine why it is not working.Maybe the uploads folder must be set to
wp-content
in Media Settings,http://example.com/wp-admin/options-media.php
, and make the first two conditionals of the functionso_8519968_custom_upload_dir
deal with the other upload possibilities (direct in Media Library -post without ID, and in another post types).