Skip to content
Advertisement

WordPress Invalid image URL when using media_sideload_image

I am using the wordpress function media_sideload_image to upload images from an external link like below:

JavaScript

But I get the error:

JavaScript

Question: How can I make media_sideload_image, work properly with this particular url which I cannot change.

Advertisement

Answer

Problem:

Its a problem with the URL. The file module of WordPress can’t recognize any file extension from the URL you’re giving it. And hence throws this error. Have a look at the source code of this function https://developer.wordpress.org/reference/functions/media_sideload_image/

Solution:

I was facing this problem for the past hour. After severe headache, I was able to use a hack around it.

All you got to do is append some random Query Param at the end of the URL that equals to the “.jpeg” or “.png”.

Example:

JavaScript

This will let the URL walk out safely from the file module without throwing any errors.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement