0

I try to set up a custom upload directory for just product images.

I use this code to create the folder 'product' in the uploads directory

function fcsp_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', 'fcsp_type_upload_dir' );

(Is from Different upload directory based on post type in a theme)

The directory is created fine but how can I get it right so it will store the product images in it?

I tried it with:

define( 'UPLOADS', trailingslashit( WP_CONTENT_DIR ) . '$args' );

The images, however, are still uploaded to the default upload directory i.e. /uploads/2019/01. What am I missing?

2
  • And what if you upload the image directly to media library and then assign it to product? Should it get moved? Jan 21, 2019 at 9:12
  • No, it is not moved from /uploads/2019/01 to /uploads/product. Was just thinking: I'm using the Media Cloud plugin to upload the images also to Amazon S3. In the end I just want to remove the uploaded images form the WordPress install after the upload to S3 is done. I can't remove them with the setting in the plugin because some mapping functions have to run first.
    – A3O
    Jan 21, 2019 at 9:23

1 Answer 1

-2
$uploads = wp_upload_dir();
$uploads['basedir'].'/product/' /**** uplode product directory path ***/
1
  • Thanks, but isn't the code from your answer just uploads everything to the /product/ folder? I need it only for the product images.
    – A3O
    Jan 21, 2019 at 11:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.