Skip to content
Read For Learn
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP

Use wp_handle_upload outside of a POST

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:

  1. Save camera info as metadata on image upload?
  2. Plupload Intergration in a meta-box?
  3. Trigger refresh for new media manager in 3.5
  4. Physical organization of wordpress media library (Real Media Library plugin)
  5. Can I upload media to a specific folder?
  6. wp_upload_dir how to get just the directory name .
  7. How to generate thumbnails when needed only?
  8. Limit image upload to one and disable audio, video and other document file types to upload
  9. How to add new tab to media upload manager with custom set of images?
  10. No Thumbnails Generated
  11. Media files exist in upload folder but not showing up
  12. Add “upload media” button in meta box field
  13. How to Require a Minimum Image Dimension for Uploading?
  14. How to upload files straight to S3 without using local storage? [closed]
  15. Extend Media Library
  16. How does WP media uploader create the 3 different sized images, and how can I duplicate it
  17. How to make “Upload files”selected by default in Insert Media?
  18. WordPress 3.5: Setting custom “full URL path to files” in the Media Library?
  19. Get $image_id after uploading with media_sideload_image()
  20. simple solution for restricting access to (some) uploads/downloads
  21. what happens to existing media files when I switch to year/month directory structure format?
  22. Reject upload of wrong-sized images using the Media Uploader
  23. WordPress Media Uploader events
  24. How to show all available images in WP’s media library when using the Polylang plugin?
  25. how to upload and allow downloads of .mobi and .epub formats
  26. Upload post thumbnail from the front end
  27. Allowing WebP uploads?
  28. Give users a maximum upload capacity; limit the number of files a user can upload OR limit the number of files per upload
  29. How to add a custom field to the media screen (image/gallery)?
  30. Can’t upload media, permissions are correct
  31. How to assign multiple file-mime-types to extension?
  32. Saving Media – Which Hook is Fired?
  33. Set limit to media upload?
  34. Is it possible to reorganize the WordPress uploads directory?
  35. WordPress 3.5: Switch back to Old Media Uploader?
  36. Media upload finished hook
  37. Users can’t upload images on frontend if they haven’t got access to the wp backend
  38. How to upload SVG in WordPress 4.9.8?
  39. How Can I Organize the Uploads Folder by Slug (or ID, or FileType, or Author)?
  40. How can I batch delete all unattached images with WP-CLI or other automated process?
  41. Create image formats with different qualities when uploading
  42. Rename files during upload using variables
  43. upload_async.php returns 500 error
  44. Which filters or actions to use after a media upload and delete?
  45. Organize uploads by year, month and day
  46. wp_delete_attachment doesn’t delete images in wp-content/uploads/
  47. Display attachments by ID in a wp.media frame
  48. Force WordPress 3.3 to use Flash uploader
  49. Use a separate custom table (not posts) to handle file upload data
  50. Upload folder is not writable, even when permissions are correct
  51. Different upload directory based on post type in a theme
  52. Media not actually deleted on disk when click “Permanent Delete”
  53. Image upload callback in new 3.5 media
  54. retrieve custom image sizes from media uploader javascript object
  55. Add inline uploader to plugin option page
  56. Protecting direct access to PDF and ZIP unless user logged in (without plugin)
  57. How to wp_upload_bits() to a sub-folder?
  58. Using same directory for storing all uploaded images on a WordPress network
  59. Insert images to post not working
  60. Is it possible to allow zip files to be uploaded in WordPress?
  61. How to restrict images in v3.5 Media Library modal to only those from a specific post id?
  62. Allow CSV files to be uploaded
  63. Contact Form 7 – process form using a PHP script, instead of mailing [closed]
  64. Get an uploaded attachments local server path?
  65. How to add more upload directories?
  66. Change WordPress upload path and URL
  67. Could a large quantity of files in the uploads folder affect performance?
  68. PNG with transparent background turns black when uploaded and resized
  69. “Add Media” only shows “Full Size” under Attachment Display Settings
  70. Differentiate Featured Image from Post Images upon Upload
  71. WP 3.3 > Still no option to enable automatic image overwrites?
  72. Where do the favicons for Media Files come from
  73. How are the year and month folders added to the uploads directory?
  74. Where does wordpress store the FTP credentials?
  75. WP 3.5 media manager – how to create a working gallery frame
  76. Custom upload directory per CPT; when removed, file not deleted
  77. What might cause a POST to wp-admin/async-upload.php to return JSON >and< HTML?
  78. How to manage a standalone media folder?
  79. Send attachments via wp_mail from temporary folder
  80. Modify featured image path to Amazon S3
  81. wp_generate_attachment_metadata returns empty array
  82. How to set an upload directory for each media type?
  83. Saving images from Gravity Forms repeatable File Upload as post attachments [closed]
  84. How do I link directly to uploaded files?
  85. Large Uploads in WordPress
  86. Is it possible to trigger some JavaScript when Media Popup is opened?
  87. How to call WP3.5 Media Library manager?
  88. Disable media library tab for non admins in uploader screen
  89. What permissions does wp-content/uploads need?
  90. Is wp_read_audio_metadata() function deprecated?
  91. WordPress monthly upload directory owner set to root
  92. wp_handle_upload() – how to upload to a custom subdirectory within uploads
  93. Settings in ‘Media > Settings’ is ignored when inserting images
  94. Set a maximum upload count for users on a specific user role
  95. can not upload file .vtt on wordpress 5.0.1
  96. Refresh wp.media after ajax call
  97. I want to replace a media file (pdf) with an updated version
  98. There’s a way to scale media (images) at 50%?
  99. Multisite, upload images directly to Amazon S3
  100. How to Reduce the Maximum Upload File Size?
Categories uploads Tags post-meta, uploads
$wpdb->update() always need a second try
post_type_link Filter – Too Few Arguments

Recommended Hostings

Cloudways: Realize Your Website's Potential With Flexible & Affordable Hosting. 24/7/365 Support, Managed Security, Automated Backups, and 24/7 Real-time Monitoring.

FastComet: Fast SSD Hosting, Free Migration, Hack-Free Security, 24/7 Super Fast Support, 45 Day Money Back Guarantee.

Recent Added Topics

  • Bug in translation system: load_theme_textdomain() returns true, files are available and accessible but the language defaults to english
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • Get the name of the template/*html file used
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
  • How to display the description of a custom post type in the dashboard?
  • Critical error on image display
  • Copying WP data and files into new install?
  • How to determine the DirectAdmin WordPress backup date?
  • How to get list of ALL tables in the database?
© 2026 Read For Learn
  • Database
    • Oracle
    • SQL
  • algorithm
  • asp.net
  • assembly
  • binary
  • c#
  • Git
  • hex
  • HTML
  • iOS
  • language angnostic
  • math
  • matlab
  • Tips & Trick
  • Tools
  • windows
  • C
  • C++
  • Java
  • javascript
  • Python
  • R
  • Java Script
  • jQuery
  • PHP
  • WordPress