[code]
function downloadImage($url)
{
require_once( ABSPATH . ‘wp-admin/includes/image.php’ );
require_once( ABSPATH . ‘wp-admin/includes/file.php’ );
require_once( ABSPATH . ‘wp-admin/includes/media.php’ );
$tmp = download_url($imageurl);
$ext = image_type_to_extension(exif_imagetype($tmp));
if (empty($ext)) {
return;
}
if (is_wp_error($tmp)) {
@unlink($file_array[‘tmp_name’]);
return $tmp;
}
$name = basename($imageurl);
$tmpnew = $tmp.’.’.$ext;
if (!rename($tmp, $tmpnew)) return;
$name = pathinfo($imageurl, PATHINFO_FILENAME).$ext;
$file_array = array(
‘name’ => $name,
‘tmp_name’ => $tmpnew
);
$id = media_handle_sideload($file_array, 0);
if (is_wp_error($id)) {
@unlink($file_array[‘tmp_name’]);
return $id;
}
$attach_url = wp_get_attachment_url($id);
return $attach_url;
}
[/code]