There’s actually a great function that will do all three of those things for you:
media_sideload_image( $url, $post_id, $description );
The first argument is the remote url of the image you want to download. The second argument is the post id of the post to which you want to attach the image. The third argument is optional, but will be a description if included.
I’d suggest finding a way to throttle the downloads so that you don’t keep timing out, such as limiting the number of images it pulls in per load and setting the importer to refresh itself between downloads, or using AJAX to do them one at a time asynchronously.
Related Posts:
- How to replace post image url before posting using api?
- How to find attachment by it’s name?
- Insert an image into a post by API
- Programatically creating image attachments from local URLs and setting featured image
- Show prev and next post links for parent post of current image in attachment page?
- How to change image url?
- media_sideload_image with rewritten urls?
- Redirect to another page using contact form 7? [closed]
- How do you import images from a URL in your post?
- Retrieve Image Attachments Getting Post Thumbnail Image First
- Is it possible to enable the ‘Link To’ field under ‘Attachment Display Settings’ for a Featured Image?
- How to get image URL from media_sideload_image?
- Custom image size not displaying with wp_get_attachment_image()
- How to echo images Urls from a wordpress post, that are relally in the post
- attachment_fields_to_save first parameter is null
- Troubles with code for upload a image
- Setting name of attachment URL
- How do I mitigate the forward slash problem for URLs of images after a WP migration?
- Images will not attach to posts after site migration
- How can I use an image from an external source without uploading it to the media library
- Filter to remove image dimension attributes?
- Stop wordpress from hardcoding img width and height attributes
- how to get original image using wp_get_attachment_image_src
- How to automatically add rounded corners to thumbnails?
- WordPress adding scaled images that don’t exist (1536×1536 and 2048×2048)
- Is there a hook which fires after all thumbnails are generated?
- Image Scaling using get_the_post_thumbnail issue in WordPress
- How do I delete all generated images from my server except those currently used in posts
- How to create thumbnails with a fixed width, so all of them will have the same width?
- How to limit number of images being printer out in “Set Featured Image” pop up?
- Get attachment ID of author_meta image – Attachment Metadata
- Black and White thumbnails
- Adding Featured Image to Post programatically
- Get an image from url in front end submission form
- Open the attachment details modal
- How do I delete thousands of unattached images?
- Why would image paths in wordpress suddenly change?
- Download button for Featured Image in every post – automatically
- Re-process Images
- Broken urls with http site and https wp-admin
- Converting Attachment to Images?
- How to check if user is uploading/setting an image as a featured image?
- Thumbnails are bigger in size than the original image
- Link to full size image or attachment page in photo theme
- add the post as canonical for attachment page wordpress
- Exclude featured image from gallery in wp-admin
- Featured images not displaying at full resolution
- Images not loading after migration
- WordPress adds extra width to post images container boxes
- Handling image uploads without thickbox
- Customizing the attachement page to show rest of all images in a gallery from post
- Update old Post image metadata to ‘thumbnail’ wp_attached_file
- WP keeps looking up post thumbnail on https even though wp-config says http
- How to replace images?
- Fixing image names for a site being restored
- Export xml use/import images from theme directory
- Get custom thumbnail sizes [duplicate]
- Call to undefined function wp_generate_attachment_metadata()
- Custom responsive ‘featured image’ sizes
- the_post_thumbnail(‘medium’) setting some images width=1 height=1
- How to set uploaded image as featured image wordpress
- What can i do add title to ‘large’ image?
- Height and width attribute not setting by the_post_thumbnail
- How to force WordPress to upscale images?
- How to set minimum required size for featured image?
- All Images not calling alt text
- Can we use featured image as third party website image URL
- Show (print) featured image all dimensions (height, width)
- add_image_size not working
- Show last 12 uploaded images on home page, but only from posts
- Serve different images per screen size and cache possible?
- Exclude image url from search query
- Product Images Low Quality After Updating From WP 3.5 to 4 [closed]
- Convert featured images into “product images”
- How to attach images that I “insert from url” into the post?
- Resize uploaded images [duplicate]
- How to use the thumbnail size with catch_that_image
- Output posts attached images as thumbnails linked to full-size with lightbox class
- Thumbnail and image management
- my featured image of wordpress are not showing in the front end though image are uploaded in media library
- How to change Gallery image url in product page ? In function.php?
- How is called this thumbnail slider feature
- How to delete original image file after upload and resize
- An error has occurred during the upload process in wordpress version 5.3.2
- Show featured image in all posts
- Import images from old WordPress site into new site
- WordPress After migration some images src is empty
- Images 404 after wp migration to a subdomain
- Custom Background for each page and post
- Using custom field to change alt text for a featured image
- List sub pages with custom field content converting image ID to URL
- How to display image thumbnails on the search results page?
- Images in plugin broken after URL migration
- Get custom size of thumbnails
- After I upload a file to library its name converted to dashes
- Surround uploaded image link with div
- Show title when inserting single images
- greater flexibility in targeting images attached to page
- Images in sub-pages fail to load
- Remove one srcset entry from wp_get_attachment_image