views:

706

answers:

5

I am using Wordpress's add_meta_box() function to add customized meta fields to the Add New Post page, like this. I want one of these fields to allow the user to upload a file, so that a single image, pdf, audio file, or video can be associated with the post.

The closest example I've seen is this one (link removed*). Unfortunately it does not suit my needs, as I want my file to be processed by Wordpress's Media Uploader - so it should appear in the Media Library afterwards, and thumbnails should be generated according to the Media settings.

I think ideally there would be a way to tap into Wordpress's existing Add Media dialog, and simply output the URL of the uploaded file into a text box, but I don't see how to do that. This question is similar, but the answers are a little clunky - I would like to keep this super simple for my end users.

How can I accomplish this? Please do not recommend plugins such as Flutter or Magic Fields - I have tried these and they do not suit my purposes (I want the images to be processed by Wordpress's Media Uploader). I am using Wordpress 3.0-alpha.

(*=link removed due to report of malware; it is in the archive, but at own risk)

+1  A: 

Take a look at Image Widget - a well made widget that does what you want, but as a widget. Maybe you can convert it to work on your Add new post page.

Another plugin you can try is Pods CMS. Might be overkill but when you do fileuploads with this, they are automaticly added to the media library.

Greenie
A: 

This WordPress Meta Box PHP Helper class might help you when working with WordPress Meta Boxes.

farinspace
A: 

I think this is what you're looking for: http://wordpress.org/extend/plugins/nkmimagefield/ Apparently it doesn't support custom post types but a little hacking should do. It already does the hard work of interfacing with the media editor.

Rafael Vega
A: 

I eventually figured this out for myself and got it working just as I wanted, and thought I would post an update for anyone who finds this question and is curious.

I did not use any plugins, but wrote a custom function to handle file uploads for my theme. I used built-in Wordpress functions such as save_custom_postdata(), wp_handle_upload(), wp_insert_attachment(), wp_generate_attachment_metadata() and wp_update_attachment_metadata().

I also had to use add_action('admin_head'...) to add some JavaScript which adds "multipart/form-data" to the form tag on the Edit Post page, to allow file uploads.

I would post my code, but it is very long and fairly specific to my project. Feel free to message me if you need it.

shipshape
A: 

Kindly email me the code at umair.vatao [the famous at sign] gmail [dot comes here] com

Umair