This should work:
$wp_upload_dir = wp_upload_dir();
$fileurl = "http://damianc.pl/th.jpg";
$filename = $upload_dir['path'] . DIRECTORY_SEPARATOR . basename( $fileurl );
if ( file_put_contents( $filename, file_get_contents( $fileurl ) ) ) {
require_once(ABSPATH . 'wp-admin/includes/image.php');
$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, $pID );
$attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
wp_update_attachment_metadata( $attach_id, $attach_data );
update_post_meta( $pID, '_thumbnail_id', $attach_id );
}
Related Posts:
- How to disable WordPress from creating thumbnails?
- set_post_thumbnail_size vs add_image_size
- the_post_thumbnail responsive srcset not populating with custom image size
- Set custom name for generated thumbnails
- New image size version only for the Featured Image file
- wp_get_attachment_image_src always returns full-sized image
- Add Title Attribute to WordPress Image the_post_thumbnail
- Can WordPress crop only the post thumbnails?
- Query the post which have a thumbnail
- Autogenerated Thumbnail compression depending on size
- How to use images uploaded with custom implementation of thickbox like the_post_thumbnail() functionality
- Media thumbnail for custom post inside post content
- Thumbnail and Featured Image With Fixed Sizes?
- List category posts – Thumbnails are not displaying
- Why am I getting errors in product.thumbnails.php? [closed]
- Is there a way to change a post’s thumbnail image(s)?
- how i can increase Featured thumbnails size?
- How check that there is post thumbnail feature image or not on wordpress?
- Force WordPress to strip/remove EXIF/IPTC metadata when resizing thumbnails
- If I Regenerate Thumbnails, would WordPress delete the existing Thumbnails and replace them with ‘new copies’?
- Is it possible to nest get_previous_post()?
- How to get cropped thumbnail source for custom post type
- Get ID of featured image using “get_post_thumbnail_id(the_ID())” – without printing to screen?
- How to offset thumbnails to the center?
- Sidebar Thumbs Squished?
- Aligning the thumbnail image
- Post thumbnail not working after regenerate
- How do get post thumbnail into plugin?
- “Medium” size image displayed instead of “thumbnail” in only one post, other posts are fine
- How to create resized versions of images added with wp_insert_attachment
- get_post_thumbnail_id() Doesn’t return value [closed]
- How To Add and Display Category Image
- get_the_post_thumbnail(‘thumbnail-name’) always returns empty string
- Can’t get post thumbnail to display in foreach loop
- Show posts image attachments
- Thumbnail is showing outside its div instead of inside it
- get_post_thumbnail does not display thumbnail
- Category page with thumb is ok but can’t do the same with tags
- how can I call a posts featured image in any template?
- how to resize thumbnail image
- Different thumbnails for index and article page
- Thumbnails not being generated for PDF files
- Thumbnail size problem Desktop vs Mobile site
- How do you code a featured image into an XML as a jpg link, instead of using a data-id number?
- How to change the format of thumbnails into wordpress theme?
- display div tag only if post has thumbnail
- Thumbnails are hard cropped not matter what
- Thumbnails regenerate wp_generate_attachment_metadata
- Featured image size always cropped bottom
- set_post_thumbnail() shows Unattached on backend under Media
- Allow ‘Set featured images’ to select multiple images
- Why wordpress doesn’t generate thumbnails?
- Force Regenerate Thumbnails Not Working
- Trouble Retrieving Thumbnail URL on Front-Page
- I would like my posts to pull my custom cropped thumbnail instead of creating its own thumbnail from Featured Image
- WordPress save thumbnail but don’t show it [closed]
- Different thumbnail size than actual picture in post
- Where is the template from the_post_thumbnail()?
- Images thumbnail not cropping square
- How do I regenerate thumbnails?
- Replacing Gutenberg Featured Image control?
- Cropped featured image replaces original image in gallery
- Featured Image Inherited from Parent Page
- How can I get a different image size, if all I have is the link?
- Show current page featured image in sidebar
- How do I set a featured image (thumbnail) by image URL when using wp_insert_post()?
- Use post thumbnail as div background
- Change post thumbnail using front end post edit form
- Featured image thumbnail sometimes in content, sometimes changes header images
- Lazyload post thumbnails
- set a chosen image-file-input as a featured image from wp frontend
- How to display first post thumbnail as a background image of a widget?
- upload or auto setup feature photo by user avatar
- WordPress returns old thumbnail size after changing media settings
- ‘Post-thumbnails’ feature does not seem to register
- What can I do if I set a gif as featured image? no thumbnail when i share on facebook
- Child Category Image
- Resizing thumbnails retrospectively
- Get thumbnails with array sizes parameter
- upload image only
- Generating very small sized thumbnails from featured image
- a tricky question about featured image
- adding image to post thumbnail from another server
- Edit Media – Edit Custom Sized Featured Images
- Add filter on certain thumbnail sizes only
- Adding thumbnails independent of images in blog posts
- Featured image size per post type
- Issue with wordpress thumnails – same thumnail displayed everywhere
- the_post_thumbnail() for displaying page feautred image?
- Copy wp_postmeta from one database to another
- How can I retrieve a Featured Image thumbnail using only the Post_title?
- Fetching full size featured image to insert in RSS Feed
- Change get_post_thumb responsively?
- Making a thumbnail if there exist a link with. jpg or .png extension in WordPress
- Hide feature image when is not populated
- Crop featured image by default
- get_the_post_thumbnail() – Invalid argument supplied for foreach() in /wp-includes/post-thumbnail-template.php on line 64
- How to get the featured image inside of a single-.php template?
- Get Post Thumbnail Outside of Loop
- Theme:Twenty Fourteen, How to remove featured image on single post page for particular category