Hi @rahulnever2far,
Not sure if it matters, but this WordPress is installed on my staging which is password protected.
Wow, thanks for this!
I was just working on a new version and couldn’t figure out why the plugin was no longer uploading photos. Come to realize my dev site is also password protected and it when protected the media_sideload_image() function is failing.
With that said, it appears when using an http password images are not able to be processed. I’ll try to solve this one.
Hi,
I just found and tested this solution from Stack Overflow and it works.
Add the following to your .htaccess.
<FilesMatch "\.(png|jpe?g|gif)$">
Satisfy Any
Allow from all
</FilesMatch>
Let me know if this works for you and I will add this to the FAQ.
This code would open access to all image files on the Staging Server. They won’t be password protected anymore which defeats the purpose of password protecting the site.
There must be a way to add images to Media Library without opening access of the all image files.
I just had a look at the code media_sideload_image(). It uses URL of an image to add an image to the Media Library. That’s why it doesn’t work in a password protected environment.
We need to use image path (instead of url) to add it to the Media Library. This way password protect won’t get into the way.
Google Programmatically adding images to media library. It may help us figure it out.
I will also look into this in my free time.
Yes, media_sideload_image() requires a URL.
Maybe if I pass the URL without the http:// it might help.
I’ll run some tests on this soon.
I’ve narrowed the issue down to the download_url() function.
Specifically the wp_safe_remote_get() on line 976 function is returning the 401 unauthorized response.
And now i’m deep in rabbit hole… Somewhere in the http class the response is becoming unauthorized.
https://developer.wordpress.org/reference/classes/wp_http/