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:
- Upload images to remote server
- Each custom image size in custom upload directory?
- Uploaded images don’t show in Media Library if there are special characters in IPTC Keywords
- Rename files on upload
- Saving the pre-sanitized filename of an attachment as the Title or Caption
- getting uploaded SVG dimensions on front-end
- How to copy and paste a picture found on the web to wordpress easily
- Get attachment ID of author_meta image – Attachment Metadata
- Alter media caption/description conflict in WordPress?
- How to Display an Image Uploaded through a Meta Box in a Template?
- Cropping an image before inserting into a post
- HTTP Error when trying to upload image
- Rename attachments during upload
- Uploading images to Media Library fails with Memory Exhausted
- How to add a media with PHP
- When Uploading JPEGs, Does WordPress Compress the Original Image?
- Uploading images: ‘ø’ get replaced with ‘ø’
- SSL: How to make customizer images Protocol Relative in WordPress?
- Insert image in comment reply
- media_sideload_image with rewritten urls?
- How to order images in a post in WordPress 3.3?
- Is there a plugin or simple way to add a multiple images metabox to a post?
- How to set a contact forms total size limit
- Change WordPress Media Manager
- All Images on wordpress site broken
- Show media url immediately after upload in media uploader
- Multiple images with Media Uploader on front-end
- How to keep WP from recompressing Full Size images
- Faster way to put images into a blog post by using Add Media dialog
- Handling image uploads without thickbox
- Alter the image, before save, of a particular image size
- WordPress automatically downscaled images larger that original
- Loose images in uploads folder
- Force minimum image size and aspect ratio when uploading images
- Missing a temporary folder.error when image is uploaded in admin Set temporary Directory – WordPress 5.2.2
- WP fails to upload files to media library, /wp-admin/async-upload.php 403 forbidden
- Add a background image for Custom Post Type archives page
- Convert uploaded GIF to non animated image automatically
- Is it possible to split Default WP Media directory to multiple Folder/Subdomain?
- Ho to add “Create Slider” option to default “Add Media” popup?
- Show image crop window on image upload
- Help to upload post attachments from Ajax
- Images in Media aren’t showing, not broken
- Upload Image using wp_insert_attachment and file url don’t have file extension
- Images uploaded to the media library has strange colors. However, in the edit screen the color is ok
- Uploaded images not displaying in network site
- Change image name during uploading sequentially
- How to limit the number of images displayed in the media window?
- How to get the Gallery form/section just like in Gutenberg block?
- How to set uploaded image as featured image wordpress
- Get images attached to a specific page
- WordPress Creates Unused (Unregistered) Image Sizes
- after migration, newly uploaded images won’t show up
- HTTP error when uploading an image
- How to change the value “uploaded to” in the media library (WordPress 4.0)
- Image upload error?
- How to add “alt” attribute for image during upload at WP front-end?
- How to use getimagesize() of image upload?
- How to change image’s author via a function when using GravityForms uploader? [closed]
- Alter the image used for a user who has not uploaded a custom image yet
- Allow users to upload multiple images to their profile
- Indicate image sizes for upload by post type
- Serving Images from subdomain in wordpress
- User uploads image as “Featured Image” but WordPress resizes it?
- Profile image does not upload
- Form File Upload Field generates multiple copies of Image Files (different sizes): Is this a WP feature or is this being done by the Plugin
- Upload error on localhost (at minimum, not yet tested online)
- Recreate the upload folder from a website without having access to the backend/cpanel/filezilla directly from the website
- How to increase media image scale limit?
- Loading and Referencing Images
- Could creation of various image sizes cause slow form submission response?
- Can’t upload Images to WordPress – Error 500 Response
- Updated wordpress missing image upload area
- WordPress 4.4.2 upload give error, can’t write is upper directory allowed to be written
- How to trigger “wp_handle_upload_prefilter” filter when uploading an image programatically?
- Change wordpress default upload path to another domain
- Broken images in media library
- Images are not getting uploaded
- Run command to optimize images after they get uploaded to WordPress
- Where to add my code to auto attach images to existing products
- How to stop a post from publishing if the file upload is not a image/gif
- Troubles with code for upload a image
- WAMP install acts different than live install when uploading images
- Set WordPress to Create different images format later, not during upload
- How can I hook and edit on the fly an image uploaded
- Assigning image to Post-Type won’t work anymore: exceeded time?
- The most of the images don’t appear, although URLs are correct
- Assign one featured image to multiple posts?
- Add two images with Sweet Testimonials
- Upload button in metabox not working
- Import existing image as a single post
- Upload unnattached image from frontend
- How do I change a Themes Header Image?
- Media Library is not loading on grid view in admin panel
- Blurry Images WordPress 5.8.1
- Media upload error
- wp_handle_upload Image sizes
- What does WordPress media uploader do to image files on upload?
- Can’t upload svg files in WordPress
- Random images from uploads folder and subfolder