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

media_sideload_image

WordPress has a media_sideload_image function. It is used to upload an image and attach it to a media library.

I accept image urls as follows:

h ** p: //s.wordpress.org/style/images/wp-header-logo.png

Rewritten URLs

Some URLs on the Internet are rewritten, for example:

http://placekitten.com/100/100

Error message:

"Unfortunately, this file type is not allowed for security reasons."

The file type is the correct jpg file, but the file extension is missing.

Adding additional MIME types does not work, in my case

I tried this function, but it does not help me, because it is a file extension that is not installed.

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 to upload the h ** p: //placekitten.com/100/100 URL using media_sideload_image or similarly to attach an image to a media library?

+4
source share
3 answers

Delving into the kernel, it looks like you need the unfiltered_upload to download 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.' )); 

In accordance with the Role and Feature Documentation :

This feature is not available for any role by default (including superdomains). The capability must be activated by defining the following constant:

 define( 'ALLOW_UNFILTERED_UPLOADS', true ); 

If this constant is defined, all roles on one site installation will be given the opportunity unfiltered_upload, but only Super Admins will be given the opportunity to install Multisite.

+2
source

I read your question yesterday when I needed this solution. I will find the answer in 24 hours.

Here is the complete 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 "; } 
+2
source

Today I ran into the same problem and came up with a slightly dirty but successful method for working. As it turned out, media_sideload_image only checks the .jpg extension (or any image) in the URL, so if you add it at the end of your link, this will work.

Therefore, you only need to add something at the end of the URL that will not change the result, for example:

 http://placekitten.com/100/100?name=image.jpg 

I canโ€™t say that it works all the time, but it works here (TESTED). :)

0
source

All Articles