php - How to set up and use a custom upload directory for post type 'product'

I try to set up a custom upload directory for just product images.I use this code to create the folder 'product

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?

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?

Share Improve this question edited Jan 21, 2019 at 9:07 A3O asked Jan 21, 2019 at 8:39 A3OA3O 1114 bronze badges 2
  • And what if you upload the image directly to media library and then assign it to product? Should it get moved? – Krzysiek Dróżdż Commented 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 Commented Jan 21, 2019 at 9:23
Add a comment  | 

1 Answer 1

Reset to default -2
$uploads = wp_upload_dir();
$uploads['basedir'].'/product/' /**** uplode product directory path ***/

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745285881a4620557.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信