Use a separate upload folder for custom post attachment upload


So, i’m trying to find out a way to use two separate upload folders, being the default one wp-content/uploads for general media uploads, and another one say wp-content/custom for one specific type of attachments (PDF files attached to one specific post_type).

It is important to keep’em separated both for organization and data security as the PDF files will hold somewhat sensitive data that should only be acessible by two custom user roles, while general media is, well, general.

I’m a little embarrassed to show you the code i got working, because it’s lousy, but here it goes:

    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

// 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... :$


I’d really rather just have 2 upload files being one for this post-format and another for the rest. Is there a cleaner way to go about it?

moraleida 2 years 2020-12-23T11:10:24-05:00 0 Answers 7 views 0

Leave an answer