Support

Account

Home Forums General Issues Save an Image using code, not GUI?

Solving

Save an Image using code, not GUI?

  • I have an Image field (source_logo) in my field group, which is deployed on Taxonomy pages in the backend, wherein the taxonomy is called “Sources”.

    Using a custom plugin I’m writing, I go through input material, for each “source”, want to save an image from a URL on the the public web… in to the source_logo Image field.

    I’ve not idea how to do this. I know how to upload an image manually in the GUI, but how can I save an image using PHP?

  • Saving an image from a URL to an attachment in the media library is not an easy thing to do and there are several plugins that focus on doing just this one thing. Examples https://wordpress.org/plugins/save-import-image-from-url/ and https://wordpress.org/plugins/add-media-from-url/. Here is topic on stack on the subject https://wordpress.stackexchange.com/questions/50123/image-upload-from-url. You might be able to use these as examples.

    Also, the ACF image field stores and attachment ID and not a URL, so you would need to have 2 fields, the first to hold the URL and the second to hold the ID. Or if you’re getting the URL from some other source than an ACF image field then you’d need just the image field to store the ID of the imported image.

    In either case, you would need to go through the process of importing this image into the media library and then once that is done you get the new attachment ID and set it to the ACF field using update_field(). But importing images from URLs is way beyond what ACF can do.

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.