#33878 closed enhancement (fixed)
New function: `get_attachment_image_url()`
Reported by: |
|
Owned by: |
|
---|---|---|---|
Milestone: | 4.4 | Priority: | normal |
Severity: | minor | Version: | |
Component: | Media | Keywords: | has-patch |
Focuses: | Cc: |
Description (last modified by )
The attached patch adds a new get_attachment_image_url()
function to retrieve the URL for a given attachment size.
Attachments (4)
Change History (14)
#1
@
9 years ago
- Description modified (diff)
- Summary changed from Get attachment url to New function: `get_attachment_image_url()`
- Type changed from enhancement to feature request
- Version 4.3 deleted
#3
@
9 years ago
- Keywords needs-refresh needs-unit-tests has-patch added; needs-patch removed
Nevermind, wp_get_attachment_url()
is not only for images. wp_get_attachment_image_url()
as suggested sounds good.
@sebastian.pisula Would you mind refreshing the patch with proper inline docs and perhaps unit tests?
#4
@
9 years ago
- Keywords needs-refresh removed
Attached a new patch with a single test for wp_get_attachment_image_url
. Probably need tests for all the other attachment functions though, which would take a while...
If #33070 gets in, wp_get_attachment_image_url
could be used there.
#5
@
9 years ago
- Keywords needs-unit-tests removed
- Milestone changed from Awaiting Review to 4.4
- Severity changed from normal to minor
- Type changed from feature request to enhancement
#6
@
9 years ago
- Owner set to wonderboymusic
- Resolution set to fixed
- Status changed from new to closed
In 34372:
#7
@
9 years ago
- Resolution fixed deleted
- Status changed from closed to reopened
I'm sorry but why do we need this function?
- It has confusing name: what's the difference between image_src and image_url? As far as I know these two are equivalent. We already had
wp_get_attachment_image_src()
, why do we needwp_get_attachment_image_url()
? - It doesn't do anything. The only difference between
wp_get_attachment_image_src()
andwp_get_attachment_image_url()
is that the former returns the image URL as an array value. Do we really need two functions that take exactly the same arguments and return exactly the same values just in a slightly different format? Looks like bloat?
#8
follow-up:
↓ 9
@
9 years ago
I see three main reasons for introducing the function:
- Developer happiness. I don't know how often I've read or written similar code to that in
wp_get_attachment_image_url()
. So instead of jumping through those hoops, just calling a function for getting the URL is nice. - More clarity. You are calling
wp_get_attachment_image_src()
, and then take the first element out of the returned array, which is the URL. If you use the second and third element, you get sizes. The last argument contains a boolean. That is pretty weird, I personally find the way that the function returns data very inelegant.wp_get_attachment_image_url()
clearly states what you get, making code more readable. - Ease of use. It's pretty impossible to know what the array returned by
wp_get_attachment_image_src()
contains without reading the documentation. It's also not super easy to know that you should use this function to retrieve an attachment URL, because what exactly does "source" mean? The new function is a lot more straightforward.
#9
in reply to:
↑ 8
@
9 years ago
Replying to Frank Klein:
You are calling
wp_get_attachment_image_src()
, and then take the first element out of the returned array, which is the URL. If you use the second and third element, you get sizes. The last argument contains a boolean. That is pretty weird, I personally find the way that the function returns data very inelegant.
Yup, I've seen people on support forums trying to use wp_get_attachment_image_src()
as a URL without realizing that it returns an array.
#10
@
9 years ago
- Resolution set to fixed
- Status changed from reopened to closed
Yeah, I agree wp_get_attachment_image_src()
is not intuitive as it returns an array, and wp_get_attachment_image_url()
makes it easier for people that do not want to read any docs (they still may get confused by the "clashing" names). Just not sure if we should encourage that :)
In any case, this has been in core for a few weeks. I should have seen it earlier.
Note that there's already
wp_get_attachment_url()
andwp_get_attachment_thumb_url()
.My suggestion would be to make those functions a wrapper for
wp_get_attachment_image_src()
and then add a second$size
parameter towp_get_attachment_url()
. Example: