Programmatically Add Images to Media Library

Recently I built a plugin for a client that utilized a front-end Gravity Form submission to create media file entries and link them to posts. Part of the plugin was the ability to take a file uploaded to the server (in this case, by Gravity Forms) and use that url to create a media entry, so it appears within the media library. Below is the code to add an image to the media library programmatically:

I mentioned this code in a previous post, where I outlined how to link Gravity Forms with ACF Gallery fields.

Updated 3-29-17: Updated the function to a class and utilized a better method for creating the file.

Featured image via Flickr CC, by Michael D Beckwith


One response to “Programmatically Add Images to Media Library”

  1. Thanks for this! Just what I was looking for 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *