• Slushman

    (@butimnoexpert)


    Maybe it exists already, but I’d like to see an API for uplaoding files into the Media Library from a plugin and a broswer that would allow you to choose files from the Media Library to be displayed through a plugin. For example, through an audio player plugin, I’d be able to upload the files to the Media Library through the plugin options page and choose those files to appear in the audio player. I’d also have the option of uploading the files directly to the Media Library and, through the plugin options page, hit a “chooser/browser” button that pulls up the Media Library and I can choose which files I want to appear in my player.

    Does any of this already exist? I’ve seen the function wp_handle_upload, but the Codex has extremely little documentation on it, which is pretty frustrating. Does that upload the files to the Media Library or just onto the server itself?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Unfortunately, it only uploads to the server.

    I’m looking for a way have wp_handle_upload place a record for the file into the media library. The only real reason is so that I can use a preset created by add_image_size.

    @butimnoexpert – if I make any progress, I’ll post back.

    I did find a way to make this happen.

    In my case, I’m using a general theme settings page, but the same should apply to a plugin.

    What I did:

    1. Created a file uploader on my settings page.
    2. Validate the file (image size, extension, etc.)
    3. Use wp_handle_upload() to upload the file and place it in the appropriate location (wherever within wp-content)
    4. Then use wp_insert_attachment() with a null 3rd parameter (post id) to keep the file unattached to a post. (wp_insert_attachment() returns the attachment id)
    5. Save the attachment’s id in my option

    Now I can use the value of the saved attachment id in my templates or functions.php file, allowing me to use functions like wp_get_attachment_image() and to use an image size created with add_image_size()

    Voila!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Media Library Browser?’ is closed to new replies.