所以,我想找到一种方法来使用两个单独的上传文件夹,作为默认文件夹wp-content/uploads
对于一般媒体上传,另一个说wp-content/custom
对于一种特定类型的附件(附加到一种特定post\\u类型的PDF文件)。
对于组织和数据安全来说,将它们分开是很重要的,因为PDF文件将包含一些敏感数据,这些数据只能由两个自定义用户角色访问,而通用媒体是通用的。
我有点不好意思向您展示我正在使用的代码,因为它很糟糕,但它是这样的:
function custom_post_type_metabox_save_function($post_id) {
global $post;
// Verify auto-save, nonces, permissions and so on then:
update_post_meta($post_id, "meta_key1", $_POST["value1"]);
update_post_meta($post_id, "meta_key2", $_POST["value2"]);
// this is where it gets uply. I change the \'upload_path\' to my desired one for this post type
update_option(\'upload_path\',\'wp-content/custom-upload-dir\');
// then upload the file to it
wp_upload_bits($_FILES["pdfexame"]["name"], null, file_get_contents($_FILES["pdfexame"]["tmp_name"]));
// and then change it back to default... :$
update_option(\'upload_path\',\'\');
}
add_action(\'save_post\',\'custom_post_type_metabox_save_function\');
我真的希望有两个上传文件,一个用于此帖子格式,另一个用于其余内容。有没有更干净的方法?
最合适的回答,由SO网友:moraleida 整理而成
我最终完全绕过wp上传系统解决了这个问题,所以现在看起来是这样的:
/*
* Define new upload paths
*/
$uploadfolder = WP_CONTENT_DIR . \'/exames\'; // Determine the server path to upload files
$uploadurl = content_url() . \'/exames/\'; // Determine the absolute url to upload files
define(RM_UPLOADDIR, $uploadfolder);
define(RM_UPLOADURL, $uploadurl);
function custom_post_type_metabox_save_function($post_id) {
global $post;
// Verify auto-save, nonces, permissions and so on then:
update_post_meta($post_id, "meta_key1", $_POST["value1"]);
update_post_meta($post_id, "meta_key2", $_POST["value2"]);
update_post_meta($post_id, "meta_key3", $_POST["value3"]);
$destination = RM_UPLOADDIR; // Determine the path to upload files
$filename = $_FILES["file"]["name"]; // Get the uploaded file name
// This separates the extension from the rest of the file name
$filename = strtolower($filename) ;
$exts = split("[/\\\\.]", $filename) ;
$n = count($exts)-1;
$exts = $exts[$n];
$newname = time() . rand(); // Create a new name
$filepath = $destination . \'/\' . $newname.\'.\'.$exts; // Get the complete file path
$filename = $newname.\'.\'.$exts; // Get the new name with the extension
// Now, if the upload was successful we save a post meta with the filename, if not, save nothing
if (move_uploaded_file($_FILES["pdfexame"]["tmp_name"], $filepath)) {
update_post_meta($post_id, "rm_martins_exame_url", $filename);
}
}
add_action(\'save_post\',\'custom_post_type_metabox_save_function\');
它比我以前的丑多了,但如果可以使用
upload_dir
滤器