WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

For example, we have some image on the external site: http://site.com/image.png

How do I upload this image to my wordpress as an image attachment by php?

Image should be copied to my uploads folder, post added, meta generated.

share|improve this question
up vote 2 down vote accepted

You can media_sideload_image() function to upload your files ex:

$upload = media_sideload_image( $url, $post_id );

or you can use a plugin like Cache Images which uploads all external images to your server as attachments.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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