add this to your for each and $filename to each file,
$wp_filetype = wp_check_filetype(basename($filename), null );
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)),
'post_content' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment( $attachment, $filename, 0 );
// you must first include the image.php file
// for the function wp_generate_attachment_metadata() to work
require_once(ABSPATH . "wp-admin" . '/includes/image.php');
$attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
wp_update_attachment_metadata( $attach_id, $attach_data );
Related Posts:
- Upload multiple images and insert them into custom html code
- Upload to a specific media folder created by FileBird Lite
- How can I add an image upload field directly to a custom write panel?
- Enhance Media Manager for Gallery
- Custom collection in media manager?
- Checking if a file is already in the Media Library
- Adding more options to the instance of an image. (Attachment Display Settings)
- How to implement add_image_size in a plugin
- Change Media Uploader default directory
- How to catch images with blank dimensions?
- How to extend LINK TO functionality in ATTACHMENT DISPLAY SETTINGS
- How to find attachment ID for first image in a post
- Having Problem On Getting WP Post Gallery Images URL
- Set first image (external) as featured image / thumbnail
- How can I view all WP generated thumbnails in Media Manager?
- Modify how gallery.js builds the shortcode [gallery ...] in tinyMCE?
- Hook event for upload image in the menu
- Bridge WordPress Media API with custom tinyMCE plugin [closed]
- Add Additional File Info (jpeg compression & file size) settings to Edit Images Screen
- Is it possible to modify the media library admin screen?
- Using the component outside the editor. select(‘core’) is null
- Add media and attach images to pages
- Is there a better way to implement responsive images than what WordPress uses by default?
- Getting media library popup in custom plugin admin page
- Save image as featured image
- How to show large image in checkout page using WP Ecommerce plugin [closed]
- Hook to get image filename when it is uploaded
- Resizing Images for a Gallery-Plugin?
- How to replicate Media Library “Add New” on Plugin Settings Page
- How to serve different thumbnails/images depending on users browser/platform
- How to enable sorting in custom media uploader in plugin
- How to Upload Images From a Folder (Not WP_Uploads)
- How to export post meta with images in wordpress
- Get attachments from a post
- Putting a resized image into media library with wp_get_image_editor(
- Display list of uploaded images, filtered by user under a specific user group
- Redirect to another page using contact form 7? [closed]
- wp_editor add media button not working
- Which is the better way to store images for the plugin?
- Run a function when new image is uploaded in media library
- Uploaded attachment not set as featured image
- How do I change the image from the default mysteryman in the WP Profile page
- Hook after attachment added and cropped
- Conditional Generation of Image Sizes using add_image_size
- how to change image of jquery slider plugin
- WP Still Generating 150×150 Thumbnail Size Even After Un-Setting Small Size in Functions.php
- Cleaning a filename after image sideloading a url that contains `%20`
- How to list all images used on a specific page?
- Screenshots on plugin page taken old [closed]
- Resize not resizing images with Capitial extension like JPG
- How to move an image in a plugin to the upload directory and make it as a media of wordpress?
- The URL of images on my website changed after being set as featured image
- How to create wordpress media library UI on plugin page
- wp.media gallery collection sometimes undefined
- Getting incorrect filepath inside custom block front-end output using @wordpress/create-block tutorial
- WordPress Favicon not Working For Images/Videos/PDFs
- Can’t load a script in my plugin page
- function add custom fields to media gallery
- Adding the image selector/uploader to an admin back page
- WordPress discards PNG file when uploading plugin file
- Hook for validating and rejecting frontend image upload
- WordPress Iframe for blog image
- Replace the WordPress Media Library Uploader
- WordPress Dropdown image select
- Changed media upload path for plugin, but generates error & shows wrong url
- How to destroy or dispose wordpress uploder/manager?
- Caption Shortcode: what filter to change the image size?
- Filter the_content() to add custom figure tags
- Issue on Getting WP Gallery Items In cpt-single.php As Attachments
- Creating custom Insert Media page
- Thumbnails for media Library images
- Remove image in post when it’s removed in media library
- How do you filter get_media_items by mime type in a custom media upload tab?
- Is it possible to convert various image types from remote URLs to WebP and then serve them immediately?
- Images not displaying on live server
- Open media box library from link
- Using “add_image_size” to register custom image sizes inside plugins not working
- Leveraging Core Functionality in Icon Upload Plugin [closed]
- Displaying an Uploaded Image as a Custom Avatar in WordPress
- Add a custom image source
- Load images from CDN and custom features to “Add Media” dialogue
- upload image to wordpress media library failed for custom post type
- Set srcset on a block image
- wp media uploader and ID3 tags
- Images stored from external API not loading
- WordPress Media Uploader not displaying image that has just been uploaded
- Show Image in Plugin Development
- Add custom CSS for plugin admin Page
- Add an action only for a specific user in a plugin
- Insert text programmatically in WordPress Gutenberg Editor
- How Can I Retrieve post ID in plugin file
- Translations not making it into Block UI but work in the rest of the plugin
- the function do_settings_section($page) generate error “Allowed memory size of … bytes exhausted”
- Where can I store data in WordPress?
- wp_enqueue_script() with unknown path and maybe symlink
- Sanitization of register_setting()
- Javascript function defined in view.js not reachable from save.js
- Media Library Issues
- How to move custom gutenberg block controls from settings to styles tab?
- audio tags no longer working