Reputation: 171
media_sideload_image
WordPress have a function called media_sideload_image. It is used to upload an image and attach it to the media library.
I accepts image urls like this:
h**p://s.wordpress.org/style/images/wp-header-logo.png
Rewritten URLs
Some URLs on the web are rewritten, for example:
http://placekitten.com/100/100
Error message:
"Sorry, this file type is not permitted for security reasons."
The file type is a correct JPG-file but the file extension is missing.
Adding extra MIME types don't work, in my case
I tried this function but it does not help me, because it's the file extension that is not set.
add_filter('upload_mimes', 'add_custom_upload_mimes');
function add_custom_upload_mimes($existing_mimes){
$existing_mimes['jpeg'] = 'image/jpeg';
return $existing_mimes;
}
Question
How do I upload the URL h**p://placekitten.com/100/100 with media_sideload_image or alike to attach the image to the media library?
Upvotes: 4
Views: 3281
Reputation: 1856
Today I have faced the same problem, and come up with a bit dirty yet successful method to work around. As it turns out, media_sideload_image only checks for the .jpg (or any image) extension in the url, so if you add it to the end of your link, it shoud work.
So you only need to add something to the end of the url that won't change the output, for example:
http://placekitten.com/100/100?name=image.jpg
I can't say it works all the time, but it works here (TESTED). :)
Upvotes: 3
Reputation: 432
I read your question yesterday, when i need this solution. I find a answer after 24 hours.
Here is Full solution
require_once(ABSPATH . 'wp-admin/includes/media.php');
require_once(ABSPATH . 'wp-admin/includes/file.php');
require_once(ABSPATH . 'wp-admin/includes/image.php');
$image_url = "http://domain.com/blog/23092839823";
$image_tmp = download_url($image_url);
if( is_wp_error( $image_tmp ) ){
echo "<br> Image Download Fail:";
}else {
$image_size = filesize($image_tmp);
$image_name = basename($image_url) . ".jpg"; // .jpg optional
//Download complete now upload in your project
$file = array(
'name' => $image_name, // ex: wp-header-logo.png
'type' => 'image/jpg',
'tmp_name' => $image_tmp,
'error' => 0,
'size' => $image_size
);
//This image/file will show on media page...
$thumb_id = media_handle_sideload( $file, $post_id, $desc);
set_post_thumbnail($post_id, $thumb_id); //optional
echo "<br> Image Save ";
}
Upvotes: 10
Reputation: 8092
Digging into core, it looks like you need the unfiltered_upload
capability in order to upload files without an extension:
if ( ( !$type || !$ext ) && !current_user_can( 'unfiltered_upload' ) )
return $upload_error_handler( $file, __( 'Sorry, this file type is not permitted for security reasons.' ));
According to the Roles and Capabilities documentation:
This capability is not available to any role by default (including Super Admins). The capability needs to be enabled by defining the following constant:
define( 'ALLOW_UNFILTERED_UPLOADS', true );
With this constant defined, all roles on a single site install will be given the unfiltered_upload capability, but only Super Admins will be given the capability on a Multisite install.
Upvotes: 3