$image_url="adress img";
$upload_dir = wp_upload_dir();
$image_data = file_get_contents( $image_url );
$filename = basename( $image_url );
if ( wp_mkdir_p( $upload_dir['path'] ) ) {
$file = $upload_dir['path'] . "https://wordpress.stackexchange.com/" . $filename;
}
else {
$file = $upload_dir['basedir'] . "https://wordpress.stackexchange.com/" . $filename;
}
file_put_contents( $file, $image_data );
$wp_filetype = wp_check_filetype( $filename, null );
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => sanitize_file_name( $filename ),
'post_content' => '',
'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment( $attachment, $file );
require_once( ABSPATH . 'wp-admin/includes/image.php' );
$attach_data = wp_generate_attachment_metadata( $attach_id, $file );
wp_update_attachment_metadata( $attach_id, $attach_data );
Related Posts:
- Uploaded images don’t show in Media Library if there are special characters in IPTC Keywords
- How can I hide media library images from general users?
- Regenerate missing _wp_attachment_metadata
- Get images attached to a specific page
- WordPress Attachments with old images
- How can I use an image from an external source without uploading it to the media library
- Programmatically get images by URL and save in uploads folder
- Upload images to remote server
- wp_get_attachment_image_src and server path
- Why is wp_get_attachment_image_src not working with my custom size (add_image_size)
- Function to call the attachment image from post
- Images dont show in grid view, but do in list view
- How to rename an image attachment filename using php
- Programatically creating image attachments from local URLs and setting featured image
- How do i upload an image and return the image id?
- Get Attachment ID from URL [duplicate]
- How to display a shortcode caption somewhere other than the_content
- WordPress function: limit size, only jpg, jpeg, limit of uploaded files per account
- Allowing post attachments without allowing to insert in text
- Applying class to links around self hosted images embedded to posts/pages
- Pulling Post Image Attachments in WordPress 3.9
- Import Images from one self-hosted WordPress install to another
- Uploading images to Media Library fails with Memory Exhausted
- Edit image preview is not displayed
- Link Featured Thumb to Attachment Page, If Possible
- How can I autopopulate titles in the media library?
- Upload thousands of images to WordPress without plugin?
- Image still linked as attachment to page even though it has been deleted
- get_attachment_id() only get id of first attached image after post update
- Different upload path per file type
- Add a new data attribute in tag
- Custom page template to display all image attachments
- HTTP Error when uploading files above 7mb unless using GD Image Editor
- wordpress media library shows empty images
- unattached media files that should be attached
- Add an attribute to attachments
- Bulk edit of EXIF data for jpeg images uploaded in the media library?
- How can I attach hotlinked images in posts/pages within the same server?
- WordPress Image Attachment using remote image
- How to upload images in a specific folder using API
- AJAX – save an $_FILES picture in media library
- In Wordress one author can not see the other’s author uploaded images through media library
- How to open default wp image editor screen dynamically?
- How to prevent WordPress scaling large images down
- Searching for any images in media library displays “no media found”
- Auto delete attachments that are older than x days
- How to get file URL from media library
- Replacing the content of an image attachment using API
- Change media URL (images)
- Add attachment for all images in post
- Does wp_get_attachment_image_src also work with non-image files?
- wp get attachment image always gives me thumbnail
- Why does WP allow to view and media item as a page
- 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?
- Rebuild thumbnails via WP Cli – how?
- Some Images Disappeared from Media Library?
- are unattached media files a problem?
- Large image not showing dimensions in Media Library
- What is the best way to associate multiple images to one page?
- Function to grab specific image IDs from media library.
- How to get the cropped image from media library?
- Random Characters in the end of every image url in post
- Fetching uploaded images using Link URL
- How to replace post image url before posting using api?
- How to get post id of first child of the same post type?
- Can i have 2 separate Galleries on 2 separate pages each go to a DIFFERENT Attachment.php file?
- Attachments without images
- How can i sorting and display image order by siz in media library?
- WordPress append -1 to the image name
- Auto delete content in specific folder inside media library
- Media Library Cleanup A Thought Exercise
- wp_insert_attachment() dupplicate attachment posts
- How to echo images Urls from a wordpress post, that are relally in the post
- Could creation of various image sizes cause slow form submission response?
- imported post image as normal image instead encoded image
- still need help-images inside wp-content/ uploads/ are not showing up in my media library and uploaded using FTP protocol
- Allow users to upload attachments to certain posts
- WordPress Link To Image Missing Dropdown
- wp_get_attachment_image_src calling wrong location
- How can I add an “Attach image to this post/page” link on the Add Media lightbox?
- Troubles with code for upload a image
- Create a custom attachment thumbnail for quick page load
- Change the image for a certain URL
- Assign one featured image to multiple posts?
- Some images don’t get linked when setting link-to to media-image
- Missing Images in Uploads Directory (Server Only)
- How can I hide previous_image_link() on first attachment page?
- How do I embed an image at actual size?
- Image media upload problem in page editing modus?
- How to access & display images from a post without textual content ( and vice versa) according to best wordpress practices
- My image rotate 90 degree
- How can we see which posts don’t have a featured image or it doesn’t exist anymore?
- Why some images are not allowed to be edited only for thumbnails?
- My subdirectory sites images (except webP) are throwing to 404 page even they are exist
- Image Attachments Query Not Working If Post Is Loaded Via Ajax
- Remove one srcset entry from wp_get_attachment_image
- Attachment tag archive