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?
I read your question yesterday, when i need this solution.
I find a answer after 24 hours.
Here is Full solution
Digging into core, it looks like you need the
unfiltered_upload
capability in order to upload files without an extension:According to the Roles and Capabilities documentation:
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:
I can’t say it works all the time, but it works here (TESTED). 🙂