changing location of uploads folder for custom post type only – not working

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

Read More

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 );

}

Related posts

Leave a Reply

1 comment

  1. The uploads_dir filter needs another implementation to successfully modifying the uploads dir/path.

    add_filter('wp_handle_upload_prefilter', 'so_8519968_handle_upload_prefilter');
    add_filter('wp_handle_upload', 'so_8519968_handle_upload');
    
    function so_8519968_handle_upload_prefilter( $file )
    {
        add_filter('upload_dir', 'so_8519968_custom_upload_dir');
        return $file;
    }
    
    function so_8519968_handle_upload( $fileinfo )
    {
        remove_filter('upload_dir', 'so_8519968_custom_upload_dir');
        return $fileinfo;
    }
    
    function so_8519968_custom_upload_dir( $path )
    {   
        // Check if uploading from inside a post/page/cpt - if not, default Upload folder is used
        $use_default_dir = ( isset($_REQUEST['post_id'] ) && $_REQUEST['post_id'] == 0 ) ? true : false; 
        if( !empty( $path['error'] ) || $use_default_dir )
            return $path; 
    
        // Check if correct post type
        $the_post_type = get_post_type( $_REQUEST['post_id'] );
        if( 'movies' != $the_post_type ) 
            return $path; 
    
        $customdir = '/' . date( 'Y/m' );
    
        //remove default subdir (year/month) and add custom dir INSIDE THE DEFAULT UPLOAD DIR
        $path['path']    = str_replace( $path['subdir'], '/downloads' . $customdir, $path['path']); 
        $path['url']     = str_replace( $path['subdir'], '/downloads' . $customdir, $path['url']); 
    
        $path['subdir']  = $customdir;
    
        return $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.

    $path['path']    = str_replace( 'uploads' . $path['subdir'], 'downloads' . $customdir, $path['path']); 
    $path['url']     = str_replace( 'uploads' . $path['subdir'], 'downloads' . $customdir, $path['url']);    
    $path['basedir'] = str_replace( 'uploads', 'downloads', $path['basedir']); 
    $path['baseurl'] = str_replace( 'uploads', 'downloads', $path['baseurl']);      
    $path['subdir']  = $customdir;
    

    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 function so_8519968_custom_upload_dir deal with the other upload possibilities (direct in Media Library -post without ID, and in another post types).