Wordpress file/image uploader in plugin

Hi Experts!

I need some simple wordpress file upload code, something like regular file uploader in Wordpress, but I want to include it in my plugin.

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Lalit ChandraCommented:
you can try the wonderfully plugin here
ZadoAuthor Commented:
Thanks, but I can't use someone else's work in my plugin, I'm looking for some 'standard' file upload code used in Wordpress plugins, I don't need anything fancy.
Lalit ChandraCommented:
Ok, take a look on the link...

Hope this will solve your queries.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ZadoAuthor Commented:
Don't know why, but nothing displays, I've done exactly what they say on that page, but no results:

	function custom_field_document_upload() {
            global $post;
            $custom         = get_post_custom($post->ID);
            $download_id    = get_post_meta($post->ID, 'document_file_id', true);
            echo '<p><label for="document_file">Upload document:</label><br />';
            echo '<input type="file" name="document_file" id="document_file" /></p>';
            echo '</p>';
            if(!empty($download_id) && $download_id != '0') {
                echo '<p><a href="' . wp_get_attachment_url($download_id) . '">
                    View document</a></p>';
	add_action('save_post', 'custom_field_document_upload'); //I called the function here, is it the right way?
	function custom_field_document_update($post_id) {
            global $post;
            if(strtolower($_POST['post_type']) === 'page') {
                if(!current_user_can('edit_page', $post_id)) {
                    return $post_id;
            else {
                if(!current_user_can('edit_post', $post_id)) {
                    return $post_id;
            if(!empty($_FILES['document_file'])) {
                $file   = $_FILES['document_file'];
                $upload = wp_handle_upload($file, array('test_form' => false));
                if(!isset($upload['error']) && isset($upload['file'])) {
                    $filetype   = wp_check_filetype(basename($upload['file']), null);
                    $title      = $file['name'];
                    $ext        = strrchr($title, '.');
                    $title      = ($ext !== false) ? substr($title, 0, -strlen($ext)) : $title;
                    $attachment = array(
                        'post_mime_type'    => $wp_filetype['type'],
                        'post_title'        => addslashes($title),
                        'post_content'      => '',
                        'post_status'       => 'inherit',
                        'post_parent'       => $post->ID
                    $attach_key = 'document_file_id';
                    $attach_id  = wp_insert_attachment($attachment, $upload['file']);
                    $existing_download = (int) get_post_meta($post->ID, $attach_key, true);
                    if(is_numeric($existing_download)) {
                    update_post_meta($post->ID, $attach_key, $attach_id);

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.