This function is completely unnecessary, and can be replaced with the official WP function called media_sideload_image:
media_sideload_image( string $file, int $post_id, string $desc = null, string $return = ‘html’ )
for example:
$new_image_id = media_sideload_image(
"https://example.com/image.png",
$post_id,
"Toms great picture",
'ID'
);
https://developer.wordpress.org/reference/functions/media_sideload_image/
Don’t forget to check the result, if this fails it will return a WP_Error object, and you must handle it. The error object contains the message telling you what went wrong, and it’s why your original code failed mysteriously ( it never checked for errors so if one happened it didn’t say anything ).
If you want to use this outside WP Admin, you’ll need to include one or two files before using it. Read the official documentation for how to do that
Related Posts:
- Image Upload from URL
- Upload images to remote server
- How to delete resized (cropped) image uploads and prevent future resizing?
- Download an image from a webpage to the default uploads folder
- After Moving a Site to Another Domain, All Images Are Lost
- Allow contributor role to upload images and not edit already published articles
- Limit image resolution on upload
- How to insert images into posts without using Add Media dialog
- Media Upload Folder – how to manage
- Upload multiple images in a custom metabox
- hook into completed image upload filter
- getting uploaded SVG dimensions on front-end
- Adding upload button in metabox
- Automating a Daily Picture Blog?
- Images uploading to wrong folder
- Replace all of post’s image URLs with upload directory URLs
- How can I add the “Use as featured image” to a custom metabox?
- How do i upload an image and return the image id?
- Restrict the number of images to upload per post
- WordPress function: limit size, only jpg, jpeg, limit of uploaded files per account
- How to increase media image scale limit?
- Frontend image uploading from edit profile page. (goldenapples)
- Filter to modify post_title after image upload?
- Uploading an image as featured image from frontend form
- Automatically Add Caption in image
- Uploading images to Media Library fails with Memory Exhausted
- Edit image preview is not displayed
- Rename image during upload using date and time stamp?
- How to disable the suffix “-scaled” which is being added at the end of each uploaded image?
- How to set a Minimum Image Dimension for Uploading
- What’s the best “insert all images” plugin?
- Is 700,00 jpgs too many for a WordPress site.?
- Different upload path per file type
- Animated Gif image not uploading correctly
- Frontend Post – Allow Only Image File Upload
- HTML tags in WordPress image caption
- HTTP Error when uploading files above 7mb unless using GD Image Editor
- wordpress media library shows empty images
- Is there a plugin or simple way to add a multiple images metabox to a post?
- How can I upload an image for background use using the Theme Editor in WordPress?
- Generating different dimension images while uploading image file from custom plugin page
- Show media url immediately after upload in media uploader
- Can’t seem to attach uploaded image to post and set it as thumbnail
- Bulk edit of EXIF data for jpeg images uploaded in the media library?
- Image upload appearing as broken images following server move
- Migration to WP – Couldn’t load image
- How can I create more upload paths, like a post corresponding to a post title folder used to store images of the same product
- Get image URL after image created from canvas and sent to WP uploads with AJAX
- How to stop thumbnail generation from some images and different size thumbnail generate
- How do I fix “Sorry, this file type is not permitted for security reasons”
- an error occurred in the upload. please try again later (Maybe File Permission Issue)
- Auto delete attachments that are older than x days
- How to implement secure frontend image upload? [closed]
- Change wp-content/uploads to uploads/%postname%/%image%
- Change media URL (images)
- Does wp_get_attachment_image_src also work with non-image files?
- Upload media only to DB
- How to get the real file type for images that do not have an extension
- Images are not showing up in “Uploaded to this post”
- WordPress Creates Unused (Unregistered) Image Sizes
- Images don’t update after being edited
- Is there a way in WordPress to convert images to WebP without a plugin?
- Images not found after migrating WordPress website
- Some Images Disappeared from Media Library?
- Error uploading images in wordpress
- Disable image rename on upload
- Capitilize Alt/Title Tag in Image HTML, Reorder HTML Output
- HTTP error when uploading an image
- How can i identify media uploaded to my website that is not being used anywhere anymore?
- Upload images – Theme options
- image URL changed in wordpress
- image upload/edit is broken: “Image data does not exist. Please re-upload the image.”
- WordPress append -1 to the image name
- Save media files (images) to database instead of the filesystem
- Recreate the upload folder from a website without having access to the backend/cpanel/filezilla directly from the website
- Trouble with Uploading and Displaying Images
- WordPress Multisite: Images moved to /sites/1?
- media_handle_upload() with 1 image under 1Mb chokes my server and returns timeout error
- How to disable post-thumbnail from generation?
- How to get attachements URL from author meta and display them?
- How to stop a post from publishing if the file upload is not a image/gif
- Troubles with code for upload a image
- What is the best way to handle Ajax image upload on front end?
- 403 Forbidden when accessing image in wordpress
- problem with updating metabox of upload image
- Missing Images in Uploads Directory (Server Only)
- Rename uploads filenames to POST-ID on upload
- Media upload in add comment meta box
- Transfered live site to new server and images are not showing
- Limit files uploaded per folder
- My image rotate 90 degree
- Bug when uploading pictures with Arabic name characters?
- What type to upload image within theme options?
- Can’t manage to display an uploaded picture (uploaded from a custom button) in the front end
- My subdirectory sites images (except webP) are throwing to 404 page even they are exist
- Uncaught Error: Call to undefined function file_is_valid_image()
- if user role is x – show image – wordpress
- “Media Library” shows as empty, despite images being present
- How do I display a PDF thumbnail as a link to the PDF without uploading the image
- Featured Image add tab