1

I am attempting to copy a file from an external URL and then reference the local copy of the image via a WordPress shortcode.

So far I have managed to copy the external image to the media library using media_sideload_image(), but am unable to find a solution to retrieve the newly uploaded file and use it within an tag.

Does WordPress offer any solution to find a file within an unknown subfolder in the media library? Or is there a method of copying an image with media_sideload_image() to a specific directory in the media library?

0

If you check the documentation for media_sideload_image() it returns HTML img tag for the image right away. Alternately it can return just the URL.

Note that while this is suitable for immediate use, it's generally challenging to go back from URL to attachment ID. IDs are much more friendly for many things, such as being stored, processed, used to access image sizes, and so on.

If there are intermediary steps in your process you might want to use deeper level media_handle_sideload(), which returns ID for the attachment rather than processed HTML.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.