Skip to main content
3 of 8
added 40 characters in body

Custom form for Media Library

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.

Right now I have it set up so that if Media Library detects the upload, this code runs that assigns categories using a variable called doctype that is based on the form selection:

 add_action( "add_attachment", "execute_on_add_attachment_event" , 10, 1);
 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(974);
        break;
 }
 wp_set_object_terms( $attachment_id, $uploadCategory, 'category' );
 }

I have created a custom file upload form function that works (see below), it copies to the YYYY/MM folder in uploads, but Media Library doesn't seem to see it. Is there some way to make Wordpress Media Library aware of it?

 function myFileUploader() {
   if (isset($_POST['submit'])) {
     wp_upload_bits($_FILES['fileToUpload']['name'], null, 
     file_get_contents($_FILES['fileToUpload']['tmp_name']));
   }
   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">news</option>
          <option value="Minutes">mins</option>
          <option value="Treasurer Report">treas</option>
       </select>
       <input type="hidden" name="doctype" value="<?php echo '$doctype';?>"/> 
       <input type="submit" value="Upload File" name="submit">
    </form>
     ';
 }
 function myFileUploaderRenderer() {
    ob_start();
    myFileUploader();
    return ob_get_clean();
 }