1
我一直試圖直接爲woocommerce產品帖子類型獲取不同的上傳,但它適用於每次上傳。這裏是我的代碼:woocommerce產品的不同上傳目錄
function custom_upload_dir($path)
{
// Determines if uploading from inside a post/page/cpt
// If not, default Upload folder is 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 from a post/page/cpt
// Save uploads in FILETYPE based folders. When using this method,
// you may want to change the check for $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;
}
這是寫我的文件到宇/目錄完美,但並不僅限於產品立柱型。任何幫助,將不勝感激。
你在哪裏調用這段代碼?鉤子在哪裏? – Skatox
函數 - 這裏是我的鉤子: – rhysclay