There is no uploading occurring here, but you did describe sideloading.
So use media_handle_sideload
instead, e.g.
$file_array = [
'name' => 'test.jpg',
'tmp_name' => '/path/to/test.jpg'
];
$post_id = 0;
$attachment_id = media_handle_sideload( $file_array, $post_id );
if ( is_wp_error( $attachment_id ) ) {
// ... it failed
}
- https://developer.wordpress.org/reference/functions/media_handle_sideload/
- based on https://developer.wordpress.org/reference/functions/media_handle_sideload/#comment-983
There are other functions that build on this, e.g. media_sideload_image
which takes the URL of an image, downloads it, and runs it through the sideload functions,
Related Posts:
- Save camera info as metadata on image upload?
- How to generate thumbnails when needed only?
- Media files exist in upload folder but not showing up
- Set limit to media upload?
- How to upload SVG in WordPress 4.9.8?
- Use a separate custom table (not posts) to handle file upload data
- Custom upload directory per CPT; when removed, file not deleted
- Modify featured image path to Amazon S3
- How to set an upload directory for each media type?
- How to get all files inserted (but not attached) to a post
- How to protect uploads in multisite if user is not logged in?
- Trigger JS when featured image upload window is opened in admin
- Modify the array of selected images in media modal
- How can I upload SVG images using the media uploader?
- Delete images uploaded by ‘Subscriber’ role
- Remove upload_files capability from a role but allow role to manage an avatar image
- 3.5 media manager add CSS / JS to new ‘tab’ iframe content
- post_id missing from the wp-admin file upload request
- Insert Featured image from Feed
- http error when uploading media files
- Image uploading stuck on “crunching”
- HTTP Error when uploading mp4 video file
- Change the size of the image preview on the media edit page
- “upload_mimes” filter not working. Need to allow WOFF and WOFF2
- Saving WordPress generated thumbnails in a subdirectory
- Add select field to media uploader that adds a class to the image
- Retroactively place uploaded media into -month, -year based folders?
- Can’t seem to upload a file to a custom directory
- wp.media add context
- Conditional add_filter for upload directory?
- Need to download uploaded binary file
- Insert Image automatically when upload finishes wordpress media uploader
- How do I modify the url of uploaded media content?
- WordPress “HTTP error.” when uploading Media – IIS
- WordPress uploads folder path. how it is decided?
- Limit upload file type on one custom post type
- Uploading images on front end doesn’t generate thumbnail sizes
- Filter / Hook to get attachment ID before uploading?
- Adding Media Upload to Custom Options Panel breaks “Insert into Post”
- PDF file upload issue
- WordPress bug with capabilities?
- Update image via WP REST API
- Media Library does not show the uploaded image
- Attach Thumbnail Generated from Video Upload as Featured Image for the Video
- ‘An error occurred in the upload. Please try again later.’ for users with different roles
- Manipulate CSV data during upload, is it possible?
- Check if uploaded file has a specific filename
- How to submit data between wp_iframe and backbone.js in media upload
- Uploading .mp3 files http error – 2 “exact” files one successful other fails
- HTTP Error after finishing uploading a video that is bigger than 64 MB -> After raising the upload limit manually in the wp-config file
- Set post_parent value for the images uploaded by Add Media button into post
- What are best practices to storing uploads in WordPress?
- Original image not saving?
- Restrict role to view own media
- Create featured image from a remote url when creating a post
- Using Add from Server to upload by post ID
- Stop image resizing in particular case – is that possible?
- Change the look of the media uploader
- Issue with images which have no sizes available
- How to use media upload on metabox post page without breaking TinyMCE?
- Uploads going to the root of wp-content/uploads
- WordPress Images, Header Videos and Carousel is not working live but perfect on Local Host Is It FTP? Filezilla?
- Uploaded image with non-english characters is named incorrectly in upload folder
- Can’t upload images (incorrect “file exceeds upload_max_filesize” error)
- Anybody knows if i could upload a leaflet or openlayers3 map to wordpress webpage?
- “Trying to upload files larger than” error will not go away
- Theoretical limit of upload file size [duplicate]
- Remove Media File Items From Server That Do Not Exist in Media Library
- How to add filetype to meta value when using wp_upload_bits?
- Double slash in upload URLs
- All my files are on my blog! I need sync solution
- Efficient way to move media folder to another folder
- Limit users to specific uploads
- WordPress blog with a custom made theme hosted on heroku
- media file uploading
- Editor User Role can’t see other users media
- How to allow .ged file uploads
- Media files not loading
- problem when uploading file by metaboxes
- What folder to place Large Video files?
- WordPress Upload Speed
- _d_improd_ directory in uploads breaking site images
- Organizing the Media Library for Cleanup
- Allow author to upload image via Media button without plugin
- Drag and Drop Media Not Working in Windows 10 Edge Browser
- Exclude some photos in media library
- Make custom thumbnail size image in media_sideload_image function
- Upload file to front-end form and send as email attachment
- uploading photos
- How to handle image resize in media_handle_sideload?
- Host Images from Link
- Upload media error: unable to create directory (windows hosting)
- Add frontend editor with media buttons for Visitors and display only their media
- Can’t upload files 1MB+ [closed]
- WordPress Custom Fonts Problem! [closed]
- I want to customize wp.media (the wordpress admin uploader) in order to change the file name but only in video custom post type
- Auto shortlink for file uploads
- Obtaining detailed error information from Media file upload process
- Is it possible to restrict the number of media uploads (photos) per user?
- How to restrict product access to personal user uploads in WordPress