I have been looking everywhere for this and havent been able to find an answer.
I need to pass a file from the url source to wp_handle_upload. How can I accomplish this?
Thank you
I have been looking everywhere for this and havent been able to find an answer.
I need to pass a file from the url source to wp_handle_upload. How can I accomplish this?
Thank you
You must be logged in to post a comment.
wp_handle_upload()
is made to handle, well, uploads. It expects upload-related inputs.To “upload” image from remote URL there is
media_sideload_image()
. Since you actually don’t specify type of your file, you might need to tinker some yourself, using this function as an example.As @Rarst said, most convenient way to handle an external upload and create the related attachment post is use
media_sideload_image()
, this function:img
html tag for the imageHowever, sometimes one may want just access to attachment post id and is not interested in attache the attachment to any post, in this case WordPress has no built-in function, so the most convenient way is copy part of core from
media_sideload_image()
and part ofmedia_handle_sideload()
:This function act as
media_sideload_image()
but return the attachment id, or aWP_Error
if something goes wrong.It takes an optional argument
$post_id
to attach media to a specific post/page, but if not passed, the attachemnt post is created but not attached to any post.