0

Often times I need to get the URL of a specific image in the media library but I am not doing it in the context of a PHP script. I need to put a element in in a widget that doesnt have WYSIWYG, or give the URL to an 3rd party affiliate, or something like that. I know I can get the original URL from the media library. Is there a pattern that that WordPress uses to get the URL of a specific image size (like thumbnail or medium)?

If I had an image in the media library at,

/wp-content/uploads/2016/10/example.jpg

... what would the URL of the thumbnail be?

I tried things like appending -thumbnail, _thumbnail, -medium, and _medium to the filename, before the extension, but none work.

1

In general the format is: originalfilename-300x203.jpg, where 300x203 are the dimensions of the thumbnail.

However, there are a few things that mean you can't rely on this: - if the user has edited the file within WP, the filename will change - if the original image is smaller than the dimensions you specify, that thumbnail will not have been generated.

I think if I needed to do this, I'd create a 'just for me' PHP file or Wordpress template in which I pass it the ID of the image and it generates the URL for me using WordPress's own image functions to which you can pass the size (e.g. get_the_post_thumbnail_url).

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.