I'm trying to create a custom form which takes an image (offsite) and attaches it to the post so I can use it as a thumbnail in the loop. I've been looking at media_handle_upload but that appears to only be working for content that's locally uploaded.


Thanks, Dennis


When you link to an external image, WordPress will reference the external URL. media_handle_upload is meant to handle uploaded images that are now living on your server. So unless you're telling the server to download the image from the external source and save it on your server, media_handle_upload won't apply.

It sounds like you're building your own custom importer. I recommend you take a deeper look at the process. A typical upload:

  • User selects file from local system
  • File uploads through form to $_POST variable
  • WordPress takes file out of POSTed form, runs it through media_handle_upload, and stores it on the server

If you're referencing an offsite image and attaching it to the post, WordPress won't do steps 2 and 3. You'll need to add your own scripts to grab the remote image and store a copy locally.

  • Thanks, but the user won't select a file from local system, it will be a url provided by a custom field :) – FLX Jun 9 '11 at 22:18
  • That's what I'm trying to tell you. If you're putting a URL in a custom field, you aren't actually uploading anything. WordPress only runs media_handle_upload on uploaded media. So if you want to use the offsite image in the loop as a thumbnail, you need to make your plugin fetch the image and store it locally on the server following the same path a file would take if the user were to upload it directly. – EAMann Jun 10 '11 at 14:14
  • Thanks EAMann, it appears media_sideload_image exists though, which should be able to perform this. I haven't been able to get it to work, but I asked a question about it here. I would really appreciate it if you could take a look :) Thanks! – FLX Jun 12 '11 at 21:04

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.