Custom upload folder

by user995317   Last Updated August 12, 2018 19:08 PM

I have custom post type and I would like that when admin/user uploads images (for example featured image) to this custom post type, the image would be uploaded to custom folder. As default they go wp-content/uploads/ but I would want them to go wp-content/uploads-post-type-name or wp-content/mu-plugins/my-plugin/uploads/

Got it done



Answers 1


Use something like this function, you need to set condition for if(user==admin) and then if(post-type=='some-post-type'):

function wpse_16722_type_upload_dir( $args ) {

    // Get the current post_id
    $id = ( isset( $_REQUEST['post_id'] ) ? $_REQUEST['post_id'] : '' );

    if( $id ) {    
       // Set the new path depends on current post_type
       $newdir = '/' . get_post_type( $id );

       $args['path']    = str_replace( $args['subdir'], '', $args['path'] );         //remove default subdir
       $args['url']     = str_replace( $args['subdir'], '', $args['url'] );      
       $args['subdir']  = $newdir;
       $args['path']   .= $newdir; 
       $args['url']    .= $newdir; 
    }
    return $args;
}
add_filter( 'upload_dir', 'wpse_16722_type_upload_dir' );
Alex P
Alex P
August 12, 2018 18:44 PM

Related Questions



Create a duplicate media attachment post

Updated January 14, 2018 12:08 PM