Upload a PDF from URL

We often have to do a lot of imports from old databases / sites. This means getting images and other files and adding them to the new site’s media library and attaching them to the new post that is related to the old site’s object / page / post. With images I always used media_sideload_image and that is easy enough.

media_sideload_image example:

So that works well enough for images. However, if you want to upload any other file types, like PDFs, you need to use a different approach.

There we go! Between those 2 functions above you can upload pretty much anything with a valid URL to your media library!

NOTE: I am also using another function called url_exists(). This is a simple function that just checks to make sure there is actually something being returned from the given URL.

FINAL NOTE: Obviously I didn’t wrap most of the above code in a function but I do usually convert the above code to a function for easier use. Feel free to set that up for yourself…or not 🙂