php - Different upload directory for woocommerce products -
i have been trying different upload directonly woocommerce product post types work applying every upload. here code:
function custom_upload_dir($path) { // determines if uploading inside post/page/cpt // if not, default upload folder used $use_default_dir = ( isset($_request['post_id'] ) && $_request['post_id'] == 0 && $_request['post_type'] == 'product' ) ? true : false; if( !empty( $path['error'] ) || $use_default_dir ) return $path; //error or uploading not post/page/cpt // save uploads in filetype based folders. when using method, // may want change check $use_default_dir $extension = substr( strrchr( $_post['name'], '.' ), 1 ); switch( $extension ) { case 'jpg': case 'png': case 'gif': $customdir = '/woo/images'; break; case 'mp4': case 'm4v': $customdir = '/woo/videos'; break; case 'txt': case 'doc': case 'pdf': $customdir = '/woo/documents'; break; default: $customdir = '/woo/others'; break; } //remove default subdir (year/month) $path['path'] = str_replace($path['subdir'], '', $path['path']); $path['url'] = str_replace($path['subdir'], '', $path['url']); $path['subdir'] = $customdir; $path['path'] .= $customdir; $path['url'] .= $customdir; return $path; }
this writing files woo/ directory perfect not limited product post type only. appreciated.
you going want overide woo default uploads directory in woocommerce plugin file class-wc-admin-post-types.php
if want override it, can create filter 'upload_dir' higher priorities.
add_action('pre_get_posts', '_my_pre_get_posts', 10, 1); function _my_pre_get_posts( $wp ) { global $typenow, $blog_id; if ( 'product' == $typenow && $blog_id != 1) { switch_to_blog(1); } }
it may or may not work due post_type
depending on current post_type being queried, if want work orders check 'shop_order'.
i tried once worked few times. might overiding before woo methods think can remove method temporarily , let script have work separately.
i found question looking similar thought may have more luck i. wp has built in $mime
action/filter. use this:
add_filter('upload_mimes','wpaclip_restrict_mimes_types'); function wpaclip_restrict_mimes_types($mimes) { if (!current_user_can('author')) { return; } $mimes = array( 'acc' => 'audio/aac', 'mp4 | m4a' => 'audio/mp4', 'mp1 | mp2 | mp3 | mpg | mpeg' => 'audio/mpeg', 'ogg | oga' => 'audio/ogg', 'wav' => 'audio/wav', 'webm' => 'audio/webm' ); return $mimes; }
this might isolate file types.
Comments
Post a Comment