Uploading Images to Media Library via wp_handle_sideload() fails

Your files array needs to mimic the $_FILES global so you need to make a dummy page with some file inputs that prints or dumps the contents of $_FILES to see how it is structured. Make a variable that looks like that with your data and pass that wp_handle_sideload().

EDIT:

This answer was correct at the time of writing and is if you wish to use wp_handle_sideload(). media_handle_sideload() allow you to attach the media to a post at the same time.

See @anatol’s own answer for a full solution.

Leave a Comment