0

I want to be able to upload files and either immediately or after upload assign multiple categories to them. I have 3 different kinds of PDF document: Treasurer report, minutes, and newsletter. What I want to be able to do is have a user assign a document type prior to the upload and have that type translate into multiple category assignments per document kind, like this:

   Treasurer: Assign Categories A, B and upload file to media library 
   Minutes: Assign Categories C, D and upload file to media library 
   Newsletter: Assign Categories E, F and upload file to media library

I am aware of wp_handle_upload but have been unable to find anything showing how to use it to set the category at the time it uploads. Is that possible or is there a better way?

9
  • can you fix the indentation on your code? There's no indenting! Also where is this code located? Is this on the frontend? Is there a specific reason to use wp_upload_bits and not one of the sideload functions such as wp_handle_upload? Are you asking how to fix your custom form or are you asking something else? Try to make your question as clear and fully self contained as possible, avoid ambiguity and don't assume people will understand or get the correct reference, if something is broken say how it is broken and how you tested it
    – Tom J Nowell
    Commented Mar 7, 2023 at 15:02
  • Edited to ask a specific question.
    – Lando
    Commented Mar 7, 2023 at 15:40
  • You've removed a lot of very useful information and made a broader question that's even more abstract and ambiguous, it made the question harder to answer! Can you edit it back in? E.g. your edits have removed all mention of a form which means now it's not obvious how you're uploading the PDFs, can you fix this? Re-add the code with fixed indentation, include the name of the file it's located in and where on your site it gets shown. Right now it looks like you're using the Add New media library page in the dashboard menu or the drag and drop in the post editor
    – Tom J Nowell
    Commented Mar 8, 2023 at 12:19
  • if you don't know how to indent code correctly that's ok, but you should say so, don't delete it :( I can't help you if you remove/hide stuff
    – Tom J Nowell
    Commented Mar 8, 2023 at 12:20
  • I am new here. My question was modified because I have no idea what methods are available and I thought it was important to give overall objective first (I see many comments on posts with code here asking the code poster what they are trying to accomplish; as I was indenting the code as you requested, I realized I was proposing a solution that I wasn't at all sure about). No preference how the file gets uploaded as long as 1) it appears in the media library once uploaded and 2) gets assigned to specific multiple categories (as determined by the user-defined type of document) when it does.
    – Lando
    Commented Mar 8, 2023 at 17:50

1 Answer 1

0

Here's how I did it, using a form to id file and set doc type, uploading with media_handle_upload, then post-processing using the attachmentID and combo box choice from the form with wp_set_object_terms:

  function myFileUploader() {
        if (isset($_POST['submit'])) {
            require_once( ABSPATH . 'wp-admin/includes/image.php' );
            require_once( ABSPATH . 'wp-admin/includes/file.php' );
            require_once( ABSPATH . 'wp-admin/includes/media.php' );
            $doctype = $_POST['doctype'];
            $attachment_id = media_handle_upload("fileToUpload", 0); 
                if (is_wp_error($attachment_id)) {
                    // There was an error uploading the image.
                    echo "Error adding file";
                } else {
                    // The file was uploaded successfully!
                    echo "AttachmentID  is " . $attachment_id  . "<br>";
                    execute_on_add_attachment_event($attachment_id, $doctype);
                } 
      }
      echo '
        <form action="" method="post" enctype="multipart/form-data">
          <input type="file" name="fileToUpload" id="fileToUpload">
          <label for="doctype">Choose a document type:</label>
          <select id="doctype" name="doctype">
            <option value="Newsletter">newsletter</option>
            <option value="Minutes">minutes</option>
            <option value="Treasurer Report">treasurer</option>
          </select>
          <input type="submit" value="Upload File" name="submit"/>
        </form>
      ';
    }
    
    function myFileUploaderRenderer() {
      ob_start();
      myFileUploader();
      return ob_get_clean();
    }
        
    add_shortcode('custom_file_uploader', 'myFileUploaderRenderer');
  1. Post-processor:

     function execute_on_add_attachment_event($attachment_id, $doctype){
     switch ($doctype) {
       case 'Minutes':
             $uploadCategory = array(946,974,933,923);
             break;
       case 'Treasurer Report':
             $uploadCategory = array(946,935);
             break;
       case 'Newsletter':
             $uploadCategory = array(946,975,922,937);
             break;
       default:
             $uploadCategory = array(946);
             break;
     }
     wp_set_object_terms( $attachment_id, $uploadCategory, 'category' );
     }
    

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.