Building a better media uploader for WordPress

I’m building a complex gallery component for a client in WordPress. My main stumbling point is creating a media uploader in the admin screen that will let them upload images into different folders. Due to the number of images, organizing by folders is critical.

I essentially need to emulate the functionality of the current WordPress media uploader, but with the ability to create/choose folders for the images.

Read More

I don’t really know where to start- Is there a standard way of implementing file uploads in websites that would lend itself to this scenario?

Related posts

Leave a Reply

1 comment

  1. You can use WordPress’s built in upload handler, wp_handle_upload(). You can use the upload_dir filter to set the custom directory.

    Here are some code snippits from one of my plugins that you can use/modify:

    public function saveCustomFields()
    {
        global $post;
    
        if($post->post_type == self::POST_TYPE && current_user_can( 'edit_post', $post->ID ) )
        {
            if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
                return;
    
            $fileReference = 'installPDF'; // This has to be in a variable because it gets passed by reference to wp_handle_upload()
    
            // save normal custom fields w/ update_post_meta() here
    
            if( empty($_FILES[$fileReference] ) )
            {
                // your custom logic if needed
            }
            else
            {
                $overrides = array(
                    'test_form' => false,
                    'unique_filename_callback' => self::PREFIX . 'setFilename'      // this lets you rename the file
                );
    
                $result = wp_handle_upload( $_FILES[$fileReference], $overrides );
    
                if( is_array($result) && array_key_exists('error', $result) && !empty( $result['error'] ) )
                {
                    // failure logic
                }
                else
                {
                    // success logic
                }
            }
        }
    }
    add_action( 'post_updated', array( $this, 'saveCustomFields') );
    
    public function addFormEnctype()
    {
        // this is needed to enable file uplodas on your form
    
        echo ' enctype="multipart/form-data"';
    }   
    add_action( 'post_edit_form_tag',   array( $this, 'addFormEnctype') );
    
    public function setUploadDirectory($uploads)
    {
        global $post;
    
        if( $post->post_type == self::POST_TYPE )
        {
            $uploads['path']    = $this->uploadDir . $this->uploadYear .'/';
            $uploads['url']     = $this->uploadURL . $this->uploadYear .'/';
            $uploads['subdir']  = '/'. $this->uploadYear;
            $uploads['basedir'] = $this->uploadDir;
            $uploads['baseurl'] = $this->uploadURL;
        }   
    
        return $uploads;
    }
    add_filter( 'upload_dir',           array( $this, 'setUploadDirectory') );