Sunday, 28 July 2019

Upload Image within Wordpress Front-End Form via the Image's URL

I am using the Board Game Geek API in order to pull in various data. One piece of data I want is the image.

For example, here is an API call for a particular board game: https://boardgamegeek.com/xmlapi2/thing?id=169786&stats=1

When I get the response via my ajax call, I am able to grab the image url within the xml via:

var image_url = $(data).find("image")[0].textContent;

However: at that point I'm stumped. I want to upload the image that is located at that url, but I do not know how to upload the image via a form.

In wordpress: the only way I know how to upload images is if the image already exists on my computer. In that scenario, I have the following input:

<label for='board_game_image></label>
<input type="file" name="board_game_image" id="board_game_image" multiple="false"></input>

This input creates the "Choose Files" button to upload an image from your computer. Once the form is submitted, I use media_handle_upload to save the image within the uploads directory and create the relevant database records. Unfortunately: this process appears to not work when the image does not already exist on your computer, but instead exists somewhere on the internet (i.e. the image exists via the url).

Question: How can I upload an image via a wordpress front-end form when the image does not exist on my computer? Instead: all I have is the url of where the image exists on the internet.



from Upload Image within Wordpress Front-End Form via the Image's URL

No comments:

Post a Comment