WordPress.org

Ready to get started?Download WordPress

Forums

importing images to the Media Library from a Database (5 posts)

  1. tyricm
    Member
    Posted 1 year ago #

    I am trying to find a way to add images to the Media Library without using POST and $_FILES.

    I am developing a plugin to integrate an image database with the WordPress Media Library. The image database returns a base64-encoded jpeg file and now I'm trying to figure out how to get that into the Media Library.

    I can save it to disk, but any attempts to upload it through wp_handle_upload() fails because the image has not been uploaded to the server through POST. I can manually add the image to $_FILES, but this will fail because it will not pass move_uploaded_file().

    Ideally, I would like to pass the base64-encoded jpeg to WordPress and have it save the file in the Media Library, but if there is a command to do this, I have yet to find it.

    Any suggestions would be appreciated.

    Tyricm

  2. bcworkz
    Member
    Posted 1 year ago #

    The primary function to use is wp_insert_attachment(), though you'll need to do a few other things to properly insert a file into the media library. I'm not that familiar with the process, but I know the path and image attributes are stored in postmeta via related metadata functions.

    To ensure you get everything right, insert an unattached image attachment (ironic that is not a contradiction in terms :) ) with the media uploader. Using phpMyAdmin, compare that entry with your own entry attempt.

  3. tyricm
    Member
    Posted 1 year ago #

    Thanks for the insight.

    I've made progress in my search and found the command wp_handle_sideload(). This allowed my to successfully copy the file to wp_contents/uploads/.

    I did not find it in the WordPress codex, which I find interesting.

    This command allowed me to copy the file, but it did not add attributes to postmeta, so I still have some work to do. Perhaps wp_insert_attachment() will help me find what I need.

    Thanks again.

  4. bcworkz
    Member
    Posted 1 year ago #

    Oooh! Sideload, that looks interesting. Would be useful to several folk. Unfortunately, there are many functions missing from the Codex. I've been doing some Codex writing lately, I've made a note to add pages for the sideload variants. They won't have any more information than what's in the source code comments, but at least the search engine will eventually index it.

    If you happen to come across any noteworthy quirks or have an useful example snippet that would help the documentation, I could add it to the page, or you can do it yourself once the framework is in place.

  5. bcworkz
    Member
    Posted 1 year ago #

    The new Codex page for wp handle sideload() is up. It can take a while for the search engine to pick it up and start returning it in searches. Until then this link is your best chance of finding the page. A link is also available from Function_Reference#Miscellaneous_Functions.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.