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');
-
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' ); }