Wordpress - Uploading Images to Media Library via wp_handle_sideload() fails
This is the surprisingly simple code that ultimately worked out to do what it's supposed to:
function oo_attach_images($images, $id){ //$images is an array of image urls, $id is the ID of the post I want the images to be attached to
require_once(ABSPATH . '/wp-admin/includes/file.php');
require_once(ABSPATH . '/wp-admin/includes/media.php');
require_once(ABSPATH . '/wp-admin/includes/image.php');
foreach($images as $image){
$array = array( //array to mimic $_FILES
'name' => basename($image), //isolates and outputs the file name from its absolute path
'type' => wp_check_filetype($image), // get mime type of image file
'tmp_name' => $image, //this field passes the actual path to the image
'error' => 0, //normally, this is used to store an error, should the upload fail. but since this isnt actually an instance of $_FILES we can default it to zero here
'size' => filesize($image) //returns image filesize in bytes
);
media_handle_sideload($array, $id); //the actual image processing, that is, move to upload directory, generate thumbnails and image sizes and writing into the database happens here
}
}
I used Anatol's code but ran into trouble when pulling images using HTTP. To fix this, use download_url first to get a local copy of the image.
It's also useful to know that media_handle_sideload returns the attachment_id number which you can use to display the image after you've uploaded it.
foreach($images as $image){
$tmp_name = download_url( $image ); // get the image and save it locally temporarily
$array = array(
'name' => basename( $image ),
'type' => 'image/jpeg',
'tmp_name' => $tmp_name,
'error' => 0,
'size' => filesize( $tmp_name )
);
$_image_id = media_handle_sideload($array, $id);
$_src = wp_get_attachment_url( $_image_id );
echo '<img src="' . $_src . '" />';
}
try
{ // only need these if performing outside of admin environment
require_once(ABSPATH . 'wp-admin/includes/media.php');
require_once(ABSPATH . 'wp-admin/includes/file.php');
require_once(ABSPATH . 'wp-admin/includes/image.php');
// example image
$image = $review['image'];
// magic sideload image returns an HTML image, not an ID
$media = media_sideload_image($image, $new_post_id);
// therefore we must find it so we can set it as featured ID
if(!empty($media) && !is_wp_error($media)){
$args = array(
'post_type' => 'attachment',
'posts_per_page' => -1,
'post_status' => 'any',
'post_parent' => $new_post_id
);
// reference new image to set as featured
$attachments = get_posts($args);
if(isset($attachments) && is_array($attachments)){
foreach($attachments as $attachment){
// grab source of full size images (so no 300x150 nonsense in path)
$image = wp_get_attachment_image_src($attachment->ID, 'full');
// determine if in the $media image we created, the string of the URL exists
if(strpos($media, $image[0]) !== false){
// if so, we found our image. set it as thumbnail
set_post_thumbnail($new_post_id, $attachment->ID);
// only want one image
break;
}
}
}
}
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}