WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've set up a custom post type and would like to have the ability to attach files to those posts. I've got the CPT and metabox set up, and the upload function as well.

When I display the CPT on the front end, I'd like there to be a download link for the file. To simplify things, I'd like to use the included MIME type icons (wp-includes/images/crystal/document.png for example). Using wp_upload_bits() there's no MIME in the meta array.

Is it possible to add the MIME type to the function? Or should I be looking at another function/method of identifying the MIME?

Here's my upload code:

 $upload = wp_upload_bits($_FILES['resources_attachment']['name'], null, file_get_contents($_FILES['resources_attachment']['tmp_name']));

 if(isset($upload['error']) && $upload['error'] != 0) {
   wp_die('There was an error uploading your file. The error is: ' . $upload['error']);
 } else {
 add_post_meta($id, 'resources_attachment', $upload);
 update_post_meta($id, 'resources_attachment', $upload);       
share|improve this question
up vote 1 down vote accepted

Ended up using wp_handle_upload(), which puts MIME type into the DB automatically. See http://codex.wordpress.org/Function_Reference/wp_handle_upload

One thing to note, this function enters the file in the DB as post meta and not as an attachment. wp_insert_attachment() would do the latter.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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