function handle_media_sideload($url, $parent_post_id = 0, $type = 'any')
{
if (!function_exists('download_url') || !function_exists('media_handle_sideload')) {
return false;
}
// if we didn't get a URL, let's bail
$parsed = @parse_url($url);
if (empty($parsed)) {
return false;
}
$tmp = download_url($url);
if (is_wp_error($tmp)) {
return $tmp;
}
// First check to see if we get a mime-type match by file, otherwise, check to
// see if WordPress supports this file as an image. If neither, then it is not supported.
if (!$this->is_file_supported_for_sideloading($tmp) && 'image' === $type && !file_is_displayable_image($tmp)) {
@unlink($tmp);
return false;
}
// emulate a $_FILES entry
$file_array = array('name' => basename(parse_url($url, PHP_URL_PATH)), 'tmp_name' => $tmp);
$id = media_handle_sideload($file_array, $parent_post_id);
@unlink($tmp);
if (is_wp_error($id)) {
return $id;
}
if (!$id || !is_int($id)) {
return false;
}
return $id;
}