Wordpress media_sideload_image - Download http://placekitten.com/100/100? Wordpress media_sideload_image - Download http://placekitten.com/100/100? wordpress wordpress

Wordpress media_sideload_image - Download http://placekitten.com/100/100?


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 ";    }


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.


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). :)