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
- How to add year and month path to old attachment images?
- Images 404 after wp migration to a subdomain
- I cannot upload images to post or the media library in wordpress
- Images missing from Blog post page
- Why image Thumbnail url is not displaying with custom size?
- Blank image thumbnails in media library
- How to disable post-thumbnail from generation?
- How to set a post featured image from an already made custom field
- featured image in custom poste type (backend)
- Why does wp_get_attachment_image_src return the wrong size, when the correct size exists?
- Custom Background for each page and post
- stop wordpress from resizing feature gif images
- second featured post not displaying on page [closed]
- Using custom field to change alt text for a featured image
- How to Make Only Images Connected to a Post Show? Not Entire Media Collection?
- Bypass wp_safe_remote_get()?
- Broken images/videos after migration – DIVI theme- works ok on Mac OS
- Get “Image CSS Class” value from the Advanced Options section
- set_post_thumbnail_size only working for width
- Which tools should I use for a gallery that uses an API and url for the path of the images?
- Uploaded images not showing properly using wordpress
- Delete post if featured image is smaller than Y pixels by Y pixels
- Theme page headers and rotator images not assigned after website migration
- How can I add an “Attach image to this post/page” link on the Add Media lightbox?
- Newly uploaded images are not updating in featured image
- List sub pages with custom field content converting image ID to URL
- wp_get_attachment_image not to get the post_thumbnaill
- How to force regenerate thumbnails in WordPress posts?
- How to display image thumbnails on the search results page?
- Declare global variable that can be used on other function
- Images in plugin broken after URL migration
- Get custom size of thumbnails
- After I upload a file to library its name converted to dashes
- Url link to feature image in the portfolio [closed]
- How can I use images that are on the server locally?
- attachment.php & flexslider—linking thumbnail to specific image
- Create a custom attachment thumbnail for quick page load
- Change the image for a certain URL
- Surround uploaded image link with div
- Extract url from next_image_link
- How can I hide previous_image_link() on first attachment page?
- Lots of attachments is causing slow load
- Search and replace image urls in posts
- How do I get resized thumbnails?
- Protocol relative url not pulling up images on IE, FF, and OPERA
- Black and White Thumbnail in WordPress Gallery
- Show title when inserting single images
- Making a thumbnail if there exist a link with. jpg or .png extension in WordPress
- Why is WordPress loading a different sized post thumbnail?
- Create post thumbnails from images stored outside of uploads directory
- Post migration – images do not display
- Broken image URL
- Upload unnattached image from frontend
- Image attachement management – a philosophical question
- wp_get_attachment_link() add rel attribute if the link points to direct image
- Title image is missing ( but is not the WP 3.5 issue )
- greater flexibility in targeting images attached to page
- Images in sub-pages fail to load
- Stop Attachment image names from taking top level permalink
- How to access & display images from a post without textual content ( and vice versa) according to best wordpress practices
- Getting top Image From the Gallery and printing out a thumbnail with Exact Dimensions
- Using the on-board image editor for featured images: edits are not being used
- get_post_custom: get image URL instead of image ID
- Tried everything but Facebook share button won’t load the post thumbnail
- has_post_thumbnail() Returns False on Scheduled Posts
- Are image addresses security relevant?
- WP is showing wrong image dimensions
- Remote Image (from URL) as Media Library element
- How can we see which posts don’t have a featured image or it doesn’t exist anymore?
- Why some images are not allowed to be edited only for thumbnails?
- I tried to move the featured image below the paragraphs but now it is displaying twice
- How to import external media items into wordpress media library (without using storage)
- WPALLIMPORT Exporting images from repeater field [closed]
- Image Attachments Query Not Working If Post Is Loaded Via Ajax
- How to get WordPress to resize images for srcset?
- Remove one srcset entry from wp_get_attachment_image
- Attachment tag archive
- Edit multiple posts featured image
- Is there a way to upscale a media library original image to the nearest whole pixel in WordPress?
- Get field added via attachment_fields_to_edit filter in Gutenberg