Custom upload directory to also change attachment meta

Please see below the filter I am using to change the upload directory for my custom post-type.

My custom post-type name is ‘download’

Read More

My new directory for uploads in my custom post-type ‘download’ is now… wp-content/downloads/

The problem is in doing this, is that I my image thumbnails are missing because the attachment meta data is looking for the thumbnail in the original directory wp-content/uploads/.

How can I adjust my filter or fix the problem so the attachment data for this custom post-type only uses the new directory wp-content/downloads/

Thanks is advance for any advice or help.

Josh

add_filter( 'upload_dir', 'my_custom_upload_dir' );
function my_custom_upload_dir( $default_dir ) {

    if ( ! isset( $_POST['post_id'] ) || $_POST['post_id'] < 0 )
    return $default_dir;
    if ( get_post_type( $_POST['post_id'] ) != 'download' )
    return $default_dir;

    $dir = WP_CONTENT_DIR . '/downloads';
    $url = WP_CONTENT_URL . '/downloads';

    $bdir = $dir;
    $burl = $url;
    $subdir = '';

    if ( get_option( 'uploads_use_yearmonth_folders' ) ) {

        $time = current_time( 'mysql' );
        $y = substr( $time, 0, 4 );
        $m = substr( $time, 5, 2 );
        $subdir = "/$y/$m";

    }   

    $dir .= $subdir;
    $url .= $subdir;

    $custom_dir = array( 

        'path'    => $dir,
        'url'     => $url, 
        'subdir'  => $subdir, 
        'basedir' => $bdir, 
        'baseurl' => $burl,
        'error'   => false, 

    );

    return $custom_dir;

}

Related posts

Leave a Reply

1 comment