Solved

Wordpress file/image uploader in plugin

Posted on 2012-03-25
4
964 Views
Last Modified: 2012-03-26
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.

Thanks.
0
Comment
Question by:Zado
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 7

Expert Comment

by:Lalit Chandra
ID: 37763154
you can try the wonderfully plugin here
http://wordpress.org/extend/plugins/wp-easy-uploader/
0
 
LVL 8

Author Comment

by:Zado
ID: 37763175
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.
0
 
LVL 7

Accepted Solution

by:
Lalit Chandra earned 500 total points
ID: 37763187
Ok, take a look on the link...
http://www.trovster.com/blog/2011/07/wordpress-custom-file-upload

Hope this will solve your queries.
0
 
LVL 8

Author Comment

by:Zado
ID: 37763223
Don't know why, but nothing displays, I've done exactly what they say on that page, but no results:


<?php
	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)) {
                        wp_delete_attachment($existing_download);
                    }
        
                    update_post_meta($post->ID, $attach_key, $attach_id);
                }
            }
        }
?>

Open in new window

0

Featured Post

Report: Liquid Web beats Amazon, Rackspace & More

A study by performance analyst firm Cloud Spectator finds that Liquid Web beats rivals Amazon, Rackspace and DigitalOcean when it comes to website and cloud application performance.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The purpose of this video is to demonstrate how to exclude a particular blog category from the main blog page. This is can be used when a category already has its own tab, or you simply want certain types of posts not to show up on the main blog. …
The purpose of this video is to demonstrate how to set up an RSS Feed on a WordPress Website. This will be demonstrated using a Windows 8 PC. Feedburner will be used for this demonstration. Go to your WordPress login page. This will look like the…

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question