0

I am working on a plugin that gets image URLs from an API, some being PNG, JPG, and others JPG without an extension. I've been trying to use a combination of download_url() and media_handle_sideload() to add the images, hoping I could then convert them to WebP and retrieve them via wp_get_attachment_url(). This part is mostly based off of this code here, which doesn't work for the images with without extensions. (e.g. this URL https://s.yimg.com/ny/api/res/1.2/5kThzlhC8ohGderVoKLJrw--/YXBwaWQ9aGlnaGxhbmRlcjt3PTcwNQ--/https://media.zenfs.com/en/evening_standard_239/b4cd0950143bb1ed165aa095f73a3315)

I've tried this solution, but that didn't fix the issue. I am able to see that the image URLs without extensions are indeed JPGs, however. I saw this by checking against $tmp rather than $imageurl within image_type_to_extension( exif_imagetype( $tmp ) ). I was also able to eventually have all image URLs end with .tmp.

Other than the problematic images without extensions, the other images do become stored in the media library. From here is it possible to use the same attachment URL to convert them to WebP and then serve them on the front end? I haven't been able to find anything concrete about utilizing imagewp() in a plugin.

Is there something I am missing? Or is my approach to this completely wrong? Essentially I am trying to display WebP versions of the images after users make a request instead of the original format they are returned as.

Thanks in advance to anyone that can help with this.

-1

To serve a webP image instead of the original format using the original image URL, you could use a .htaccess redirect rules to redirect any request to the original image to the webP version.

WebP Converter for Media Does a good job at this with files uploaded to WP Media Library.

5
  • Down votes but no explanation! Not that helpful! – MichaelHabib Jan 8 at 13:34
  • Thanks for trying to help! Since it's a plugin, I won't have access to the users' .htaccess files. It would all have to be through WordPress. :-\ – whatifthis Jan 8 at 14:20
  • Have you looked up how to edit wp htaccess file from wp plugin? It can be done ! – MichaelHabib Jan 8 at 23:04
  • Oh that's news to me. Interesting. (stackoverflow.com/a/36180870/8972174). But that is kind of outside the territory of this plugin. Do you know how dangerous it may be to do that? – whatifthis Jan 9 at 16:58
  • Why would it be dangerous? Some of the most popular security and utility plugins edit the htaccess file to restrict access to sensitive info and redirects. – MichaelHabib Jan 10 at 1:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.